Starting dir

  • Follow


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
0
Reply daik.no-spam (46) 12/7/2005 9:22:44 AM

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. 
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
Reply my_email_is_posted_on_my_website (4730) 12/6/2005 11:32:38 PM


>  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

0
Reply jfbriere (107) 12/7/2005 5:21:26 AM

Daniel wrote:
> 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
-- 
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/
0
Reply nobody89 (1419) 12/7/2005 8:27:34 AM

"Daniel" <daik.no-spam@mds.nospam.mdh.se> schrieb im Newsbeitrag 
news:9d9dp117ihlemkr66uma4uhvt3tois3dli@4ax.com...
> 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 


0
Reply Hiran.Chaudhuri (21) 12/7/2005 8:43:45 AM

>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. 

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
0
Reply daik.no-spam (46) 12/7/2005 10:50:32 AM

> 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

0
Reply jfbriere (107) 12/7/2005 5:40:07 PM

>>  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
0
Reply daik.no-spam (46) 12/7/2005 7:51:29 PM

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

>Daniel wrote:
>> 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

0
Reply daik.no-spam (46) 12/7/2005 7:53:28 PM

Daniel wrote:

> 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

-- 
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
0
Reply SeeMySites (3836) 12/8/2005 12:31:30 AM

Daniel wrote:
> 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

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
0
Reply Francesco 12/8/2005 10:10:29 AM

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

>Daniel wrote:
>> 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
>
>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

0
Reply daik.no-spam (46) 12/9/2005 6:16:03 AM

11 Replies
35 Views

(page loaded in 0.124 seconds)

Similiar Articles:


















7/24/2012 1:55:05 AM


Reply: