2

I wrote desktop program in Java on Windows 7 and hanged it on startup by writing to registry the path of jar file (kind of C:\Users\User\Documents\My App.jar) in HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Run branch. When my program loads with Windows, it must load some text file that placed in the same folder as the program:

File f = new File("text.txt"); // without full path to file
if(!f.exists())
    JOptionPane.showMessageDialog(null, "File not found: " + f.getAbsolutePath());

but cant do it and I get message: "File not found: C:\Windows\system32\text.txt". It appears as if the program is located in the system32 folder and a text file, respectively, too. What did I do wrong?

cybersoft
  • 1,453
  • 13
  • 31

4 Answers4

2

My theory: if you installed Java for Windows via the traditional installer, in addition to the place you told it to put it (canonically, JAVA_HOME), the installer drops a java.exe in the system32 directory, so it's likely that when you're starting the JVM on startup, system32 is the working directory and that's where it will look for files with relative path names like the one you've provided.

The easiest solution is to specify the path to the text file absolutely in your code. I would also recommend specifying the full path to java.exe in your registry key (I would guess that right now it's just java.exe with no path) so that you can guarantee which version you're running; if you have multiple versions of Java installed only the most recently one installed will have java.exe in system32 and without a qualifying path I would guess that's the one you're getting since PATH will probably be minimal at that point.

As a closing--unrelated to your problem--point, I hate that Java does this on Windows and wipe that copy of java.exe out immediately, and then set up PATH to make sure the version I want is the one that is executed on demand.

Omaha
  • 2,262
  • 15
  • 18
  • _Java for windows drops a java.exe in the system32 directory_ No, it is in _Program Files/Java/jdk#/bin_ – BackSlash Sep 12 '13 at 18:24
  • It will install all of the binaries into the traditional "JAVA_HOME" space too, but it will also put a copy of ``java.exe`` into ``system32``. http://stackoverflow.com/questions/11063831/what-is-the-difference-between-system32-java-exe-and-program-files-java-jdk1 http://stackoverflow.com/questions/8223511/how-to-prevent-that-java-exe-is-installed-in-windows-system32 – Omaha Sep 12 '13 at 18:27
  • It is true and I think it is why you are ending up in ``system32`` as a working directory when it launches at startup. – Omaha Sep 13 '13 at 11:11
0

You need to get current working directory and use it as a part of absolute path to a file.

Take a look at this question: Getting the Current Working Directory in Java

The other workaround would be to read this registry key you described and using this information to read a file from desired path - read/write to Windows Registry using Java

Community
  • 1
  • 1
wlk
  • 5,695
  • 6
  • 54
  • 72
0

The path that you are referring is the working directory, and is not where the application is installed. Like if you put a shorcut to it, you can change the working directory and it will change the result you are getting. But as you put it to startup automatically, probably windows is setting this as the working directory.

Like that: Use registry to startup a program, and also change the current working directory? 1

You can try to pass the file directory to your app with a command line parameter in the registry.


1 - This probably won't solve the problem for you, don't seems you have one, its just an explanation

Community
  • 1
  • 1
Diego C Nascimento
  • 2,801
  • 1
  • 17
  • 23
0

I think that @Omaha's answer is describing what is going on (you are launching java.exe from the system32 directory - the JRE installer places a copy of java.exe in system32 to make it easier for users to access).

The actual solution to your situation is to adjust your CurrentVersion\Run registry entry so it specifies a default working folder for the application. Check out: Use registry to startup a program, and also change the current working directory?

Community
  • 1
  • 1
Kevin Day
  • 16,067
  • 8
  • 44
  • 68
  • This link didn't help me if I did it right. May be because of jar, not exe – cybersoft Sep 13 '13 at 11:14
  • How did you adjust your CurrentVersion\Run key? You also may want to try explicitly running javaw.exe to launch the JAR (instead of relying on file associations)? Something to try... – Kevin Day Sep 14 '13 at 00:15