Relative paths and Inno setup
Hey,
I'm creating a game in Java and during distribution (installation) I copy a directory of level files (which can be edited by the user also; hence I cannot embed them or anything) to the installation directory. The directory is called "levels", so if the executable* is in "C:\Program Files\Game\game.exe" then the directory is at "C:\Program Files\Game\levels\...".
When the game loads, I want to load one of those levels. But, the problem is that I don't know their absolute paths.
From within the IDE, I have been using relative paths (starting the path at "\levels") and this worked fine, but it is no longer working after compiling and installing the game.
When I print the paths used, from within the IDE it tells me "C:\NetbeansProjects\...", the correct path. But after installation, running the executable*, it starts at "levels" so is incorrect.
I searched for this but the only thing I found that seemed useful was using System.getProperty like this:
Code:
String exePath = System.getProperty("user.dir");
Now I got a similar, but different problem. In the IDE it tells me the correct path to the folder. Also, when I browse to the installed executable file and run that manually, it also returns the correct path. But, when I run the executable from a shortcut (from the Start menu actually, a shortcut anywhere else works fine), it suddenly tells me "C:\Windows\System32" ???
* This may be important. I wanted to use Inno Setup to create the installer, and to get it to work properly I decided to convert the JAR file to an EXE file, using Launch4J (if anyone knows it..). Hence I'm talking about executable and not about jar files.
Now, in Inno Setup I have the following code in the script which I believe takes care of creating a start menu entry:
Code:
[Icons]
Name: "{group}\GameName"; Filename: "{app}\GameName.exe"
It creates the start menu entry fine, and runs the game, but for some reason the getProperty("user.dir") suddenly returns the System32 folder... Why is that? How can I get around this?
Re: Relative paths and Inno setup
"user.dir" property refers to the user's current working dir. Which is the path from where he started the JVM (Not necessarily the home path).
I can't really understand your problem. You can use relative paths in distribution. But if you insist on using an absolute path, try the MyClass.class.getResource("MyClass.class") it returns the url (can be converted into a path - an absolute path)
Re: Relative paths and Inno setup
I can use relative paths in distribution? I tried but it didn't work...? How do you do that then?
If I use your suggestion then it doesn't even work in the IDE (I didn't try it outside) as it gives me the path to the class file, which is not really what I need. I suppose I could do some string operations on it to get the right path, but I'd rather not, especially if I can use relative paths...
Re: Relative paths and Inno setup
The path to the class file contains the path of the jar file. try the File.getParentFile() method to instead of doing string operations.
If your class is com.Nick.Thissen.SomeClass then SomeClass.getParentFile().getParentFile().getParentFile().getParentFile() should be the location of the jar file