File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes user.dir Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "user.dir" Watch "user.dir" New topic
Author

user.dir

Grant Hale
Greenhorn

Joined: Sep 04, 2011
Posts: 6
Hi.

I wrote an executable jar that needs to, among other things get the current working directory (cwd). Whenever I get this property the cwd always seems to default to the C:\Windows\System32 directory.

OS: Windows 7 - 64 bit
Java version: 1.6.0_21

The strange thing is, it worked before, meaning the correct cwd was retrieved. I haven't executed this jar file for a weeks now, so something must've changed OS-wise. I'm assuming some windows update? I really dont know. Please help

I dont have this issue on Windows XP at all by the way.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10536
    
    9

Welcome to the Ranch.
Where is your jar located?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40071
    
  28
. . . and how are you invoking it?

This sounds too complicated for "beginning", so I shall move the discussion.
Grant Hale
Greenhorn

Joined: Sep 04, 2011
Posts: 6
Hi.

My jar file is in my lib directory (\AppFolder\lib). The correct cwd is found when I run my application in Netbeans, but as soon as I run it outside of the IDE the cwd defaults to C:\Windows\System32. Its almost as if the JVM cannot find the cwd in Windows 7 and just starts in C:\Windows\System32.

I'm invoking it using
Grant Hale
Greenhorn

Joined: Sep 04, 2011
Posts: 6
Hi.

My jar file is in my lib directory (\AppFolder\lib). The correct cwd is found when I run my application in Netbeans, but as soon as I run it outside of the IDE the cwd defaults to C:\Windows\System32. Its almost as if the JVM cannot find the cwd in Windows 7 and just starts in C:\Windows\System32.

I'm invoking it using:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40071
    
  28
I meant, how are you invoking the .jar?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10536
    
    9

This is very strange. Have you set the classpath? Maybe you can write a small class which just prints out System.getProperty("user.dir"), save it on say your Desktop and run it from the command line to check what it gives?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40071
    
  28
You can set the classpath for the .jar in its manifest file.
Grant Hale
Greenhorn

Joined: Sep 04, 2011
Posts: 6
I use a manifest file for all of my jars. Upon execution of my application I print the cwd using a message box. I've executed my app within netbeans, from the command line and by directly executing the jar.

netbeans and command line --> correct cwd

direct jar execution --> Mr System32

EDIT:
This really sounds like some OS issue to me, mainly because of the fact that my application worked correctly a few weeks ago without any code changes.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40071
    
  28
I presume you are executing the .jar by clicking it (or its icon) and you have an association of .jar → "javaw"? As Maneesh asked, what is the classpath and what does it say in the manifest file about classpath?

You may be right about an OS problem. What did you get as user.dir previously?
Grant Hale
Greenhorn

Joined: Sep 04, 2011
Posts: 6
Yes, by clicking on it, and the file association for it is javaw.

My Manifest File


Previous user.dir:
- Previously, I was able to execute my application from any directory so the user.dir would change accordingly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40071
    
  28
Don't know any more. Sorry. Anybody else?
Grant Hale
Greenhorn

Joined: Sep 04, 2011
Posts: 6
Thanks Sheriff Campbell.

Appreciate the effort
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40071
    
  28
You're welcome, and sorry I couldn't help more.

The only thing is, if you double-click a desktop icon or similar and start javaw, is there a "current" directory at all?
Tracy Hiltbrand
Greenhorn

Joined: Feb 21, 2005
Posts: 1
If it's working in NetBeans, you've probably got the "Working Directory" property set. To check, right click the project in NB, choose Properties, click the "Run" category. I'm betting that the Working Directory property is set to a specific directory. If not, NB would automatically set it to the project directory.

So, either way, when NB runs your JAR file for you, it passes the option "-Duser.dir=PathToWorkingDirectory".

So if you want to produce that same behavior from the command line, just use that option. For example:

java -Duser.dir=PathToWorkingDirectory -jar YourJarFileName.jar

Hope that helps!
Armando Manzali
Greenhorn

Joined: Aug 01, 2013
Posts: 1
Hi all,
I'm new to forum but I find a similar issue and perhaps I can give more information, although not solving the problem.

I encountered a different behaviour if the java.exe executed is the one in System32 or the one in %JAVA_HOME%\bin.

If I execute "java -jar MyJar.jar" with default settings, working directory is always System32.

If I change the path var or I remove java.exe from System32 "java -jar MyJar.jar" (executing %JAVA_HOME%\bin\java.exe instead of C:\Windows\System32\java.exe), working directory is the directory from which I execute the command.

Hope this help

Bye

Armando
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: user.dir