Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

user.dir

 
Grant Hale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11087
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
Where is your jar located?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and how are you invoking it?

This sounds too complicated for "beginning", so I shall move the discussion.
 
Grant Hale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant, how are you invoking the .jar?
 
Maneesh Godbole
Saloon Keeper
Posts: 11087
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set the classpath for the .jar in its manifest file.
 
Grant Hale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know any more. Sorry. Anybody else?
 
Grant Hale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sheriff Campbell.

Appreciate the effort
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic