1

Im making a basic .bat file that runs the application. This is to get auto startup feature.

However for some reason System.getProperty("user.dir") does not always get the correct path to the program.

Basically i am saving this to the .bat file:

protected final String fileSeparator=System.getProperty("file.separator");
out.println("@echo off");
out.println("start " + System.getProperty("user.dir") + fileSeparator +"App.jar");
out.println("exit");

On Windows server it returns the correct path but on Vista it does not.

Any ideas how i could get this to work on all versions of Windows?

Alosyius
  • 8,771
  • 26
  • 76
  • 120

2 Answers2

3

You can do this by providing user.dir when you start your Java program like this

java somepackage.Main -Duser.dir=C:/Users/myUser
Amr Gawish
  • 2,015
  • 1
  • 17
  • 29
3

user.dir = User working directory [from documentation ]

It is difficult to take decision based on value of this variable. Depending on the program starting the "java", it could have different values. For example, a bat file could have different working directory from invoked from different command-windows.

You could use %~dp0 to get location of the batch script and then put other paths relative to this.

Another option is to use tools like launch4j, which allows an easy way to control program directory : How to get the path to the executable when using launch4j?

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143