1

This is my first Stackoverflow question.

I searched across Google for getting the current file name in Java. Most of the sources tell users how to find the current file name if the file is a JAR file, but I'm asking for if the current file is an EXE file.

I saw one EXE answer in Get name of running Jar or Exe, but I don't think it worked.

I use the JSmooth EXE wrapper (launch4j somehow didn't work for me), and Java 8. Is there a straightforward solution to my question? Also, it's nice to explain how it works, or provide a link to the Java documentary about it.

EDIT: To clarify, let's say that I made a Java program and used a JAR wrapper, and I named the resulting EXE "test.exe". I want the Java program be able to give me the current directory of "test.exe", including the filename itself (test.exe).

ANOTHER EDIT: Just to clarify more, go onto your desktop, create a text file, and put some text in it. Save it, and change the text file to an EXE file. Then, try to open it. Windows will give an error. Notice how the title of the message dialog is the file path of the opened file. That is the type of output I want.

Thanks.

Community
  • 1
  • 1

2 Answers2

2

Per the JSmooth documentation,

JSmooth also makes some special variable accessible for your application.

Form                          Meaning
${EXECUTABLEPATH}             Replaced by the path to the executable binary. For 
                              instance, if the executable binary launched is located
                              at c:/program files/jsmooth/test.exe, this variable 
                              is replaced with c:/program files/jsmooth

${EXECUTABLENAME}             Replaced by the name of the executable binary. For 
                              instance, if the executable binary launched is located
                              at c:/program files/jsmooth/test.exe, this variable is
                              replaced with test.exe 

You set these in JSmooth under the "Environment Settings" (the last panel), which allows you to map the variable name. So,

MY_EXECUTABLEPATH=${EXECUTABLEPATH}
MY_EXECUTABLENAME=${EXECUTABLENAME}

In your application, you can get those with

String execPath = System.getProperty("MY_EXECUTABLEPATH");
String execName = System.getProperty("MY_EXECUTABLENAME");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • It didn't work. Are there any configuration changes in JSmooth when wrapping the JAR to use these special variables? –  Jul 16 '14 at 01:24
  • @SystemSecurity2009 Edited. Try it with the curly braces. If there are, the documentation doesn't mention them. – Elliott Frisch Jul 16 '14 at 01:28
  • By the way (this is optional), if others have the same problem but don't use JSmooth, is there a way to solve this problem? –  Jul 16 '14 at 01:35
  • @SystemSecurity2009 I don't think so, not with pure Java. – Elliott Frisch Jul 16 '14 at 01:45
  • Thanks for the info. Also, in JSmooth, you can set the environment variables, where one variable's value is ${EXECUTABLEPATH} (name can be anything), and another one is ${EXECUTABLENAME}. Then in Java, you can just do System.getProperty(environment variable name) to get it (or something like that). –  Jul 16 '14 at 01:48
  • You have to go in JSmooth to the last page, where you can set custom environment variables. Then you have to add a new one, where name is EXECUTABLEPATH and value is ${EXECUTABLEPATH}. Then, you can use System.getProperty("EXECUTABLEPATH"); You can't do that if you haven't set the variable yourself yet. –  Jul 16 '14 at 01:53
0
public class JavaApplication1 {
  public static void main(String[] args) {
       System.out.println("Working Directory = " +
              System.getProperty("user.dir"));
  }
}

This will print a complete absolute path from where your application has initialized. It is used to get only the DIRECTORY.

If you are using a .exe why do you not create a installer? You can use Inno Setup, this way you are able to specify where do you want to store your .exe, and get it from your application just passing your custom directory

Victor Laerte
  • 6,446
  • 13
  • 53
  • 102
  • I just want to get the current directory and the current exe file, in case the user changes the name. If it's only one EXE file, there's no point in using Inno Setup to create an installer, but good idea, though. –  Jul 16 '14 at 01:16