1

This likely isn't possible, but I thought I would ask just to be sure. I have a batch file which starts my java app using relative paths. Ie. i have:

Application\start.bat Application\lib*.jar

My application creates a configuration file in the Application directory. My batch script uses relative paths to point to the lib directory jars. This has worked really well for me because I can move the program wherever I want and it will just run. However now I would like to be able to call the same app from command line as well not just from a shortcut which has the working directory set to Application. The problem is that I want to be able to call my application from any directory on the command line and right now this doesn't work because the working directory will be different.

I know I can always add another parameter to my app for the working directory but then I still have to create a batch script with a hard coded path to my application. Is there any way around this in Java, for example to get the directory that my main java file is in on top of the working directory? Is there a launcher app perhaps?

I can't bundle my app as a jar because it creates configuration files which I want to be in the same directory as the application.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
Coder
  • 1,315
  • 2
  • 19
  • 40
  • Duplicate of: http://stackoverflow.com/questions/6250034/using-java-find-installation-directory-of-an-application – Stephen C Sep 28 '12 at 13:58

2 Answers2

3

Consider just changing current dir in start.bat:

@cd /d %~dp0
java ...

This would change it to the folder where script is located.

pushd/popd commands can also be used to preserve current dir for calling script if needed.

Alternatively getClass().getProtectionDomain().getCodeSource().getLocation() can be used from java to get path to jar/classes.

There is also path-independent approach with config path system property:

java "-DconfigDir=%~dp0" ...
Vadzim
  • 21,258
  • 10
  • 119
  • 142
  • so getClass().getProtectionDomain().getCodeSource().getLocation() would return the file system path such as c:\Application? Does it work consistently for desktop apps? What does it do for a web app? I will give this a try when I can, thanks for the suggestion. This sounds like it might do what I want. – Coder Sep 28 '12 at 17:15
  • `getClass().getProtectionDomain().getCodeSource().getLocation()` work both for jars and unpacked classes, both for desktop and web apps. But it requires permission check under SecurityManager (if any). It returns URL. See http://stackoverflow.com/questions/2166039/java-how-to-get-a-file-from-an-escaped-url. – Vadzim Oct 01 '12 at 05:23
  • But it would recommend using a one of simpler solutions with `%~dp0`. Linux alternative is also present: http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in. – Vadzim Oct 01 '12 at 05:32
0

There is no portable solution that is going to work in all cases. Indeed, the notion of an application directory does not necessarily make sense; e.g. consider applets and applications launched using WebStart.

If the notion of an application or installation directory does make sense, then the simplest way to support it is to have the application's launcher / launch script pass it to the application Java code via an application specific -D property.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096