0

I am creating an exe file using Launch4j plug-in in Gradle. My intention is to get the directory where I am while executing the exe file that is placed in another directory. For example, the exe file is placed in "C:\Utilities" and my current directory is "C:\temp". When I execute "C:\Utilities\my.exe", I would like to get "C:\temp" in my main method.

I tried this to send the result of %CD% as a system property -

createExe {
    mainClassName = 'com.eldho.mp3tools.Mp3ToolsMain'
    jvmOptions = ['-Dworking.dir="%CD%"']
}

But, it is turning up blank inside my method.

I also tried this inside the method -

Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
logger.info("Current relative path is: " + s);

This gives the directory where the exe file is (C:\Utilities).

Any help is appreciated.

Do Will
  • 711
  • 1
  • 9
  • 18
  • Are you trying to get the path where the exe is being run from? If so why not try a more Java-centric approach? This - https://stackoverflow.com/a/227640 - allows you to get the location of a Class. What does it output when running from your exe? Worth a try. – Jakg Nov 13 '18 at 00:42
  • I am looking for the directory where I am while running the exe that is in a different directory. – Do Will Nov 13 '18 at 17:09

1 Answers1

0

A possible solution is to 'null' out the chdir property of the Launch4j configuration, by default it uses '.' but that is only interpreted once the wrapper starts your Jar file.

launch4j {
   mainClassName = 'com.eldho.mp3tools.Mp3ToolsMain'
   chdir = ''
}

Discussed in this issue

Balázs Eszes
  • 166
  • 1
  • 6