Hello, this may be alittle bit off topic, but I am not sure where to
post, so if you happen to know of a better place to find what I am
after please let me know.
My problem is that I have written a java application that lets the
user double click on a file and the program starts and loads the file.
To achive this I have wrapped the java app into a .exe file.
It all works, kind of. I set the file associations (windows as os btw)
during the installation. But what happens is that when the user double
click on a file the application starts with the directory of that file
as the "working directory". This causes problems for the applicaiton.
So I wonder is there a way to ensure that the working directory is
infact what we count on it to be?
I could run such code in the constructor or possibly even my main
method. Either way I need to make sure that the program starts in the
desired folder. Can I do this, and if so, how can I do this?
regards
Daniel
Roedy Green - 06 Dec 2005 23:32 GMT
On Wed, 07 Dec 2005 10:22:44 +0100, Daniel
<daik.no-spam@mds.nospam.mdh.se> wrote, quoted or indirectly quoted
someone who said :
>This causes problems for the applicaiton.
>So I wonder is there a way to ensure that the working directory is
>infact what we count on it to be?
In windows you can create a "Shortcut" which specifies the exe, and
the working directory, and an icon. If the user clicks that instead,
you can set the working directory to what you want.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Daniel - 07 Dec 2005 00:53 GMT
>On Wed, 07 Dec 2005 10:22:44 +0100, Daniel
><daik.no-spam@mds.nospam.mdh.se> wrote, quoted or indirectly quoted
[quoted text clipped - 7 lines]
>the working directory, and an icon. If the user clicks that instead,
>you can set the working directory to what you want.
yes, BUT the problem is that the user is not starting the program via
a shortcut, but rather by clicking on a file associated with the
program (very much the same way that you double click a .html file and
firefox starts..). Unfortunatley I can not make the association to a
shortcut and have the shortcut set the starting dir. So the problem
remains.
I briefly thought about putting the installed dir in the a Preferences
node and on startup just set user.dir to that, but I am not sure that
would actually solve the problem. I did try to set it to something
absurd, but the program did it's checks for dependecies and they all
passed. So that means that if I can't just change the user.dir and it
will work.
regards
Daniel
Jean-Francois Briere - 07 Dec 2005 05:21 GMT
> To achive this I have wrapped the java app into a .exe file.
The solution might be in the way you wrapped your java app.
Could you please explain a bit how do you achieve the wrapping?
Regards
Daniel - 07 Dec 2005 09:55 GMT
>> To achive this I have wrapped the java app into a .exe file.
>
>The solution might be in the way you wrapped your java app.
>Could you please explain a bit how do you achieve the wrapping?
I doubt the wrapping is the problem. None the less. I used launch4j
(available from sourceforge) with just plain vanilla settings. Nothing
strange, nothing changed so to say.
regards
Daniel
Jean-Francois Briere - 07 Dec 2005 17:40 GMT
> I doubt the wrapping is the problem. None the less. I used launch4j
On the contrary since JAVA would not help you for this kind of thing.
With launch4j, there is an option to change current directory to the
executable location.
Did you set it?
Regards
Thomas Weidenfeller - 07 Dec 2005 08:27 GMT
> during the installation. But what happens is that when the user double
> click on a file the application starts with the directory of that file
> as the "working directory". This causes problems for the applicaiton.
Rewrite your java application so that it does not depend on the current
working directory in order to function. Such application behavior is
almost always a bug.
Use mechanisms like Class.getResource()/Class.getResourceAsStream() in
case your application needs to find its own components, e.g. icons.
Use configuration data, e.g. via the Preference API (or Properties), and
provide a means to set this configuration data, in case the application
needs to know files, directories, locations, etc. outside its own realm.
Use the configuration data to form absolute pathes to files in case you
need to be sure that you really get a particular file.
> So I wonder is there a way to ensure that the working directory is
> infact what we count on it to be?
There is nothing Java can do about this. It is your operating system
which decides to change the working directory, not Java. Java does not
provide an API to change the current working directory, since the
concept of a current working directory does not even exist on all
operating systems. You can get the working directory via the system
property "user.dir", but that's all about it.
> I could run such code in the constructor or possibly even my main
> method. Either way I need to make sure that the program starts in the
> desired folder. Can I do this, and if so, how can I do this?
You are approaching the problem from the wrong end. Fix your application.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Daniel - 07 Dec 2005 09:55 GMT
Yeah, I guess you were right. Trying to fix this the easy way may just
prove to be the stupid way (it most often is actually...). I guess I
was just looking for the easy way out. oh well.
regards
Daniel
>> during the installation. But what happens is that when the user double
>> click on a file the application starts with the directory of that file
[quoted text clipped - 30 lines]
>
>/Thomas
Andrew Thompson - 08 Dec 2005 00:31 GMT
> Yeah, I guess you were right. Trying to fix this the easy way may just
> prove to be the stupid way ..
From my understanding of the problem requirements (file associations,
.exe's and Java application resources) JWS whould be a 'good way'.
Once JWS has delivered it all to the client, you can simply
Class.getResource() your .exe/.dll.
HTH

Signature
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
Hiran Chaudhuri - 07 Dec 2005 08:43 GMT
> during the installation. But what happens is that when the user double
> click on a file the application starts with the directory of that file
> as the "working directory". This causes problems for the applicaiton.
> So I wonder is there a way to ensure that the working directory is
> infact what we count on it to be?
Can you modify the application? Actually it should not be a problem for the
application itself to run with a different "current directory".
If it is and you cannot change it, then you better let your users know the
application has such restrictions and they have to start it via a shortcut.
Anyway you have an installation procedure, so that one can create the
shortcuts as well.
Hiran
Francesco Devittori - 08 Dec 2005 10:10 GMT
> Hello, this may be alittle bit off topic, but I am not sure where to
> post, so if you happen to know of a better place to find what I am
[quoted text clipped - 14 lines]
> regards
> Daniel
If I got it right, you need to know the complete path to the directory
where your .class file is.
I do it that way (assuming this code is in class MyClass in package
mypackage:
private String getFullPath() {
ClassLoader loader = ClassLoader.getSystemClassLoader();
String sep = System.getProperty("file.separator");
String clss = "mypackage" + sep + "MyClass.class";
URL url = null;
try {
url = loader.getSystemResource(clss);
} catch(Exception e) {
e.printStackTrace();
}
return (url != null ? url.getPath() : new String());
}
Is it what you need?
Francesco
Daniel - 08 Dec 2005 20:22 GMT
In a way that is what I am trying to achive. I had not thought of
going that way to find it but was considering using the Preferences
node to set the path during installation and then during execution
just get it out of the Preferences node. I will consider this and see
if it does indeed solve my problem.
thank you
>> Hello, this may be alittle bit off topic, but I am not sure where to
>> post, so if you happen to know of a better place to find what I am
[quoted text clipped - 36 lines]
>
>Francesco