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

7
  • Those answers only get me the location of the executable file, not the location of the cmd from where it was executed. Commented Aug 13, 2019 at 17:11
  • File f = new File(""); System.out.println(f.getAbsolutePath()); Commented Aug 13, 2019 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. Commented Aug 13, 2019 at 17:12
  • 1
    See the answer here Commented Aug 13, 2019 at 17:13
  • 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? Commented Aug 13, 2019 at 17:21

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

12 Comments

Again, this gives the directory of the executable file, not the directory of the cmd when the file is executed.
@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.
Ok, will try that, thx
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
Try to call the jar rather than the exe, so we can narrow down the error.
|
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

2 Comments

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.
hmm, different to what I'm getting. Maybe its the way I'm creating the .exe file

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.