1

How would you find the current directory which the CMD is currently in from a java file which has been exported and converted into a .exe file.

I'm not looking for the current working directory as that only gets me the location of the .exe file.

System.getProperty("user.dir") is not what I'm looking for. Same with new File(".")

If the cmd is C:Users/User/Desktop and I then call file.exe, I would like to know the path C:Users/User/Desktop, not the directory of the file.exe.

I convert the Main.java file to an .exe by: (Using Eclipse) Export as a Runnable Jar File a s file.jar

Launch4j - Outputfile = file.exe - Jar runtime path = Jars/file.jar - Don't wrap jar, launch only

Sam
  • 88
  • 7
  • Those answers only get me the location of the executable file, not the location of the cmd from where it was executed. – Sam Aug 13 '19 at 17:11
  • File f = new File(""); System.out.println(f.getAbsolutePath()); – Rohit Suthar Aug 13 '19 at 17:12
  • It still only gets me the directory from where the executable file is, not where it is executed from by the cmd. – Sam Aug 13 '19 at 17:12
  • 1
    See the answer [here](https://stackoverflow.com/a/22350931/1410303) – SomeDude Aug 13 '19 at 17:13
  • Same result as the other 2. – Sam Aug 13 '19 at 17:15
  • 1
    I think converting to .exe file might be the reason for your troubles, as it might be that it changes the current directory and that is the reason you'd get surprising results. If you use a java .jar and not an exe file, does getting the cmd directory then work as expected? – eis Aug 13 '19 at 17:21
  • @Sam Please edit your question to include the detailed description of how you create the `.exe` file. Also include the source code you have and the output you get, called from various locations in your file system. – Progman Aug 13 '19 at 17:31

2 Answers2

2

As mentioned here You would use:

System.getProperty("user.dir");

It will give you the users working directory.

Output is:

C:\Foo> java -jar bar\baz.jar
Directory: C:\Foo

Because this isn't working for you the problem must come from Launch4J.

When you're starting Launch4j there is a little dot at the Option Basic -> Change Dir, remove it and you'll be happy.

Change Dir Option

For Reference here is my Launch4J Config:

<?xml version="1.0" encoding="UTF-8"?>
<launch4jConfig>
  <dontWrapJar>false</dontWrapJar>
  <headerType>console</headerType>
  <jar>C:\Foo\bar\baz.jar</jar>
  <outfile>C:\Foo\bar\baz.exe</outfile>
  <errTitle></errTitle>
  <cmdLine></cmdLine>
  <chdir></chdir>
  <priority>normal</priority>
  <downloadUrl>http://java.com/download</downloadUrl>
  <supportUrl></supportUrl>
  <stayAlive>false</stayAlive>
  <restartOnCrash>false</restartOnCrash>
  <manifest></manifest>
  <icon></icon>
  <jre>
    <path></path>
    <bundledJre64Bit>false</bundledJre64Bit>
    <bundledJreAsFallback>false</bundledJreAsFallback>
    <minVersion>1.6</minVersion>
    <maxVersion></maxVersion>
    <jdkPreference>preferJre</jdkPreference>
    <runtimeBits>64/32</runtimeBits>
  </jre>
</launch4jConfig>

Another possible workaround would be to use a .bat file like:

java -jar bar\baz.jar

And wrap this bat into an exe file.

twobiers
  • 1,057
  • 1
  • 11
  • 27
  • Again, this gives the directory of the executable file, not the directory of the cmd when the file is executed. – Sam Aug 13 '19 at 17:19
  • @Sam Maybe your *.exe is part of your problem. You might try that with a jar file. If you need to have an exe, don't wrap the jar inside the exe, just use the exe as a launcher (I think Launch4j has an option for that) and your problem might be solved. – twobiers Aug 13 '19 at 17:28
  • Ok, will try that, thx – Sam Aug 13 '19 at 17:32
  • I converting it without wrapping, but I still get the directory of the .exe file, not the directory which the cmd is currently in, when I execute the .exe file – Sam Aug 13 '19 at 17:48
  • Try to call the jar rather than the exe, so we can narrow down the error. – twobiers Aug 13 '19 at 17:57
  • @Campingenieur when I call the file.jar, I'd doesn't output anything. I've exported the .java file as a .jar file And the .java file is String path = System.getProperty("user.dir"); System.out.println(path); – Sam Aug 13 '19 at 18:08
  • @Sam Well that is strange I've build a jar file only with ```System.out.println(System.getProperty("user.dir"))``` in main too and when I call the jar from directory ```foo``` via ```java -jar bar/Test.jar``` the output is ```foo```. So this must be an issue from launch4j. – twobiers Aug 13 '19 at 18:15
  • @VinayHegde Your solution uses the exact same approach, so it would work too. – twobiers Aug 13 '19 at 18:15
  • Idk, I'll figure it out another time – Sam Aug 13 '19 at 18:21
  • @Sam Take a look at my edit, I've found a solution. – twobiers Aug 13 '19 at 18:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197963/discussion-between-campingenieur-and-sam). – twobiers Aug 14 '19 at 19:42
  • Removing the chdir option worked for me. – Alberto May 08 '21 at 13:33
1

This should work :

import java.util.*;
import java.lang.*;

public class GetExePath
{
  public static void main(String args[]) {
    try{
      String exePath = System.getProperty("user.dir");
      System.out.print("exe path at ="+exePath .replace("\\", "/"));
    }catch (Exception e){
      System.out.println("Some Exception ="+e.getMessage());
    }
  }
}

Output :

D:\vinay_hegde\javaexample>javac GetExePath.jav

D:\vinay_hegde\javaexample>java GetExePath
exe path at = D:\vinay_hegde\javaexample
Vinay Hegde
  • 1,424
  • 1
  • 10
  • 23
  • Sorry, but it is giving me the directory of the executable file, not the directory of the cmd from where the file was executed from. – Sam Aug 13 '19 at 17:19
  • hmm, different to what I'm getting. Maybe its the way I'm creating the .exe file – Sam Aug 13 '19 at 17:21