Java get current file name EXE - java

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.

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");

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

Related

System.getProperty("user.dir") alternative

I would like to write JSon files in my Java Application, it works perfectly in my IDE but as soon as I move the directory, the system fails to write the file. I eventually found the problem : The line System.getProperty("user.dir") returns a bad path.
The files are located in C:\wamp\www\myProject during development. Here is my code at the moment :
String url = System.getProperty("user.dir") + "\\src\\json\\Crc.json";
//Returns "C:\wamp\www\myProject\src\json\Crc.json"
After moving my project to C:\Users\myUser\Desktop :
String url = System.getProperty("user.dir") + "\\src\\json\\Crc.json";
//Returns "C:\Users\myUser\src\json\Crc.json"
I would like to have a way to find where my project directory is on the computer at anytime. Do you guys have a solution ?
The C:\wamp\www\myProject folder is just a random place on your hard disk.
Java supports 3 generic places to look for resources:
the current working directory. this is where your command prompt is and what System.getProperty("user.dir") returns, but you cannot rely on that beeing somehow related to any cretain place in the file system, especially not related to the project structure on your own system.
You should only use that if your program has a command line interface and looks for some default file name to work with.
the user home This is what you get when calling System.getProperty("user.home"). On Unix this resoves to $HOME and on Windows to %USERPROFILE%.
This is the best place for files changed at runtime or holding user specific content.
the own code location. Resources in the same package as your class are accessed with getClass().getResource("filenameWithoutPath") But usually you place resources in a special folder in the application root and access it like this: getClass().getResource("/relative/path/from/src/root/filenameWithoutPath").
In your IDE this special folder should be Project/src/main/resources (according to the maven Standard Directory Layout
This is appropriate for some global configurations that you change when creating the delivery package.

how to define a file path in java to make it ready for production phase?

I am writing a program in java with netbeans IDE which receives a jasper report *.jrxml and then displays the report for the user. I wrote the following line of code for the file path
String reportSource = "src\\jasper-reports\\report.jrxml";
but when I move the dist folder in some other place and try to run the jar file inside it, my program can not find the report.
my problem is that were should I put the *.jrxml file and how to define it's path in my program so that when I want to give my software to someone else it runs without any errors (e.g. the program can find the file)
avoid using absolute paths. try to include the file as a resource in your netbeans project. then in your code you can search for and load the file as
new InputStreamReader((Main.class.getResourceAsStream("/report.jrxml")))
something like that depending on where the file resides in your project
it's more recommended using one of the two approaches:
pass the locations/paths as a -Dproperty=value in the Java application launcher command line http://www.tutorialspoint.com/unix_commands/java.htm
store it the locations/paths in a configurations file with a unique key and edit the file accordingly for different environments,
e.g.this files always stored in ${HOME}/config_files/ directory
absolute paths considered a bad practice

Jar File and file Dependency

I have to write a Java Project with a Swing GUI and some smaller formal requirements like a button which does something or methods which implement something, very basic.
I have written a programm with a basic GUI including a Jtable which stores information about students(name,semester...) in a Java File.
The Problem is that the path of this java-File in my programm is relative, in my case:
public static final String pfad = "src/abgabe/";
public static final String name = "Uni";
File file = new File(pfad + name)
If I export my Programm to JAR and send it around, it wont work on other computers because only I have the Java File.
Can I change my code to make the Jar File creates a new Java file in its own root directory?
Can I change my code to make the Jar File creates a new Java file in its own root directory?
Even if you can, you shouldn't. Most OS manufacturers have been saying for years that application data should not be stored in the apps. installation directory.
Instead put the file in a sub-directory of user.home. That will be a path that:
Is reproducible.
The app. should have write permissions for.
Although the question is not directly related, the answer is, so see also How can an app use files inside the JAR for read and write?

How do I set a working directory for Java application?

I've written a simple java app, say, with the following code:
String currentDir = new java.io.File(".").getCanonicalPath();
javax.swing.JOptionPane.showMessageDialog(null, currentDir); //This line shows a graphical dialog with the current dir
When I run it through the terminal, it gives me the directory where the jar-file is located. But when I run it using the GUI file manager (that is, right click on the jar-file -> Open With -> OpenJDK Java 7 Runtime) - the working directory is my user home directory (/home/angstrem). How can I set the working directory to be the one, in which the jar-file is situated?
You can do this:
String jarPath = YourClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
replacing YourClass with an actual class defined in your jar.
You can then make your file paths be relative to jarPath, and your program will work regardless of its working directory.
You can't, and you shouldn't be able to. Consider multi-threading for example. You can only make your application current-path-insensitive.

Java - Find absolute path of file

I need to find the path to the user's vlc.exe file.
How can I do this?
I read this http://docs.oracle.com/javase/tutorial/essential/io/find.html and tried using code like
PathMatcher match = FileSystems.getDefault().getPathMatcher("glob:vjlc.{exe, jpg, png}");
Path filename = FileSystems.getDefault().getPath("vjlc.exe","");
if(match.matches(filename))
{
System.out.println(filename);
}
and
File fil = new File("vlc.exe");
System.out.println( fil.getAbsolutePath() );
neither of which worked
I believe you are trying to do something that is not quite right.
First, you're assuming that vlc.exe exists on the local machine. But what happens if it doesn't?
Second, what happens if VLC decides at some point (new build comes out, or upgrade) to change the exe file name to vlc2.exe?
To deal with this kind of dependency, I suggest you'll pass the vlc file location as a program argument to the main() method.
This way, you can create a batch file that tries to locate the vlc.exe path, and pass it through to the java program.
Another alternative, is to setup an environment variable, that will be set up during the installation of your java application. The installation can search for vlc.exe path, or have the user to set it up. Once the variable is set, the java program can read it from the system arguments (see this example).
A third way is to have a setting files (*.ini like), that will contain the vlc exe path. You can then have the file modified according to the relevant path, and have the java program read from it (as property file). The file can be auto generated too, during the installation process, or manually edited post installation.
You can use getAbsolutePath() function.
I think you're looking for ways of searching for the vlc.exe executable on the PATH. If so, something like the following should help:
String path = System.getenv("PATH");
String pathSeparator = System.getProperty("path.separator");
for (String pathElement : path.split(pathSeparator)) {
File file = new File(pathElement, "vlc.exe");
if (file.isFile()) {
// vlc.exe exists in this location.
}
}
When a user runs VLC installer to install VLC media player under Windows, the installer creates a Windows registry key entry HKLM\SOFTWARE\VideoLAN\VLC\InstallDir. You can retrieve the path stored in the key using Java as follows:
http://www.davidc.net/programming/java/reading-windows-registry-java-without-jni
read/write to Windows Registry using Java
If the HKLM\SOFTWARE\VideoLAN\VLC\InstallDir key is present, you know VLC is installed. If the user decides to install VLC at a different directory than what is suggested by VLC installer as default, the key will be able to tell you that.
This only works when the user installs VLC through its installer. But, it won't work if the user simply extracts VLC from its zip distribution file since this approach won't touch the Windows registry.

Categories

Resources