This question already has answers here:
Run exe which is packaged inside jar file
(6 answers)
Closed 5 years ago.
In eclipse, if you have a .exe file in the package how would you run it? I know if it is outside you would just add in the path, but what would the path be for something inside the project?
Lets imagine a File f = new File(Path);
If in that case we have the file inside our project (same directory),then we dont need to add the path, just the file name and extension (ex: .txt)......I guess in your case it would be something similar.
Related
This question already has answers here:
Reading a resource file from within jar
(15 answers)
Closed 2 years ago.
I made a javafx project, when I finished I made a executable jar file. When I run it I get this error:
java.io.FileNotFoundException: src\Radiatorlista.txt
This is how my code looks like:
File radiatorok = new File("src/Radiatorlista.txt");
I know that this path will not work. I don't know how to specify the correct relative path.
This is how my jar file looks like.
The answer is that you need to put your txt files (or whatever you wan't to open) in the folder where your jar file is. For example my jar file is here:
F:/Example/for/my/file
You need to put your files in the file folder.
Then change your code like this:
File file = new File("Mytxt.txt");
After this your program should work.
This question already has answers here:
How to read text file from classpath in Java?
(17 answers)
Closed 3 years ago.
I want to read the configuration file outside the jar, how should I do?
I tried classloader.getResourceAsStream("config.xml") and succeeded while I am running the code inside Intellij. Now I want to build the jars to a folder and place the config.xml under the same folder, not inside the jar, but the program fails to detect the config.xml.
Is there a graceful way of reading the config.xml instead of using File with relative path in the code, which doesn't work while debugging/running inside the IDE?
Yes, turn it into a system property, and provide it to anything running any Java process/application.
Let's say you have a config.xml file located inside /some/path/down/the/line/, then you can do: java --classpath ... -Dapp.config=/some/path/down/the/line/config.xml tld.domain.Application.
Then all you have to do in your Java code is to reference that name/path: final String configFile = System.getProperty("app.config");, and use any well-known routine to read it from there.
Basically, you have to make sure the file/path/location is provided somehow to the Java classpath.
This question already has answers here:
How to create a folder in Java?
(8 answers)
Closed 6 years ago.
I would like to write a Java Programm which should be able to create a Folder in Windows, but the Client should be a able to decide on the Name of the Folder. How can I do that?
Thanks for any suggestions!
A small google search would have given you the answer. If you want to name it a specific way, just grab the name from a JTextField or something like that.
Create a File object and use mkdir() to create a new folder.
File folder;
folder = new File("path/to/new/folder");
folder.mkdir();
Obviously you will need to check if the folder already exists and you will need to use an inputted string for the name of the folder.
This question already has answers here:
How do I load a file from resource folder?
(21 answers)
Closed 6 years ago.
I have following project structure:
src
--Main.java
--resources
----Users.txt
And I am trying to create a file from Main.java like that:
File file = new File("resources/Users.txt");
However, I never succeed at doing so.
Why?
If it is a regular file outside a .jar, you are using a relative path. That means, the path to the file is formed from the path where you are calling the file from + the relative path. To make it work, you should invoke java within src folder
This question already has answers here:
Jar file name form java code
(2 answers)
Closed 9 years ago.
I need to get the application's directory with name. For example, if my java application is working at C:\Program Files\example1.exe I need to get it as "C:\Program Files\example1.exe". Or is it working at C:\Windows\example2.exe
I need to get it as "C:\Windows\example2.exe" . How can I do it?
You can use System.getProperty("user.dir") to find where is your working directory.