I'm trying to save a YAML formatted file packaged in the JAR to the user's disk.
The file is named config.yml and sits directly under one of the project's source folders. I can confirm that the file is packaged in the JAR with WinRAR.
I am using the following code to get the file from the JAR:
File file = new File(this.getClassLoader().getResource("config.yml").getPath());
And I am using this code to copy the file to a directory:
FileUtils.copyFileToDirectory(file, this.getDataFolder());
The getDataFolder() method is implemented by a reliable 3rd party API.
However, when I use a file instance defined with the same getDataFolder() File instance and the path "config.yml":
new File(this.getDataFolder(), "config.yml")
The console logs a FileNotFoundException.
The file path given in the stack trace is this: "file:\C:\Users\Evan\Desk
top\Test%20Server\plugins\lottocrates-1.0.jar!\config.yml" which seems correct. I tried opening the file with run prompt, which I was able to open the JAR with, but not the config.yml file.
Related
getClass().getResource works fine in Eclipse and returns the path for a file I want to have access to, but after exporting the JAR file my files don't load anymore.
I already tried to print out my path; in Eclipse the console shows:
/C:/Users/.../eclipse-workspace/GameJumpAndRun/bin/file/save1
...but if i run the JAR file by using cmd the message is
file/save1, and no files have been loaded.
System.out.println(getClass().getResource("/file/save"+i).getFile());
file = new File(getClass().getResource("/file/save"+i).getFile());
I need the file as a File and not as a BufferedReader; is there a way to get the same file in both Eclipse and the exported JAR?
I work with NetBeans IDE an I have a .txt file saved in src/myapp folder. If I run from the IDE, this recognise my
File file=new File("src/myapp/mytext.txt");
But if I build the jar file and double click it or launch it from command line I get this error:
java.io.FileNotFoundException: src\myapp\mytext.txt
I could insert absolute path, but how can I run my jar independently by the position of my project in the computer?
You can obtain the file path indepently of its position with the following:
ClassLoader classLoader = getClass().getClassLoader();
String path = classLoader.getResource("mytext.txt").toString();
Java is expecting to find the file relative to your working directory. So by hardcoding the file in src/myapp/mytext.txt you are expecting the user of your application to have the
file under the same folder structure.
If you are expecting the file to be at the same level of your jar file, you can just use ./mytext.txt. Do not put your mytext.txt under the src in your project. That is for sources you want to compile and/or bundle inside your jar file. In NetBeans move it outside the /src folder, that way when you run the program from your IDE or when you run it externally from your Jar file you find the file at the same level.
If you want the user to be able to specify the location himself of the file, you can also read the command line arguments (the arguments to your public static void main(String[] args)).
there is no such problem
File file=new File("./src/myapp/mytext.txt");
File defaultCss=new File(this.getClass().getResource("application.css").getFile());
PiChart.getScene().getStylesheets().add("file:///" + defaultCss.getAbsolutePath().replace("\\", "/"));
The above line in Controller.java fetches the required resource in Eclipse while running, but when exported to an executable JAR, it is not fetching the file.
Because:
In Eclipse the line fetches
src/com/piscope/application.css
In JAR, the path is:
com/piscope/application.css
Please let me know the path to be set so that one can run both eclipse and JAR executions without errors.
Note: Since the file is the source for the software package,the file needs to be inside the JAR file.
Just use the following (no need to format the syntax by yourself):
PiChart.getScene().getStylesheets().add(this.getClass().getResource("application.css").toExternalForm());
How would you read a file into a program that's compiled into a jar next to it through its local directory? The type read would be a simple .txt file.
It depends on what the usage of the program is. Do you know how the jar is supposed to be executed? When you try to run it, does it spit out a "usage: somejar firstarg secondarg" type message?
Also, if its a jar that you've compiled and you know how it should be executed, then you may have forgot to set its main class or manifest.
Check this: http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
If you want to read a file that exists within an external .jar file, you will need to unzip the .jar file first in your code and then retrieve the file. You can do this using Java's zip APIs. See this answer if this is the case: Easiest way to unpack a jar in java
If you want to read a file that is in the same .jar file that your code is executing, you can get the file as a resource. See this answer: Get a resource using getResource()
If the file is simply in the exact same directory as the executable .jar, create a new file like so:
File input = new File("myfile.txt");
I have an application that creates a temporary mp3-file and puts it in a directory like C:\
File tempfile = File.createTempFile("something", ".mp3", new File("C:\\));
I'm able to read it by just using that same tempfile again.
Everything works fine in the Eclipse IDE.
But when I export my project for as a Runnable jar, my files are still being made correctly (I can play them with some normal music player like iTunes) but I can't seem to read them anymore in my application.
I found out that I need to use something like getClass().getResource("/relative/path/in/jar.mp3") for using resource files that are in the jar. But this doesn't seem to work if I want to select a file from a certain location in my file system like C:\something.mp3
Can somebody help me on this one?
It seems you dont have file name of the temp files . When you was running your program in eclipse that instance was creating a processing files, but after you made a runable you are not able to read those file that instance in eclipse created, You runable file can create its own temp file and can process them,
To make temp files globe put there (path + name ) entries in some db or property file
For example of you will create a temp file from the blow code
File tempfile = File.createTempFile("out", ".txt", new File("D:\\"));
FileWriter fstream = new FileWriter(tempfile);//write in file
out = new BufferedWriter(fstream);
the out will not be out.txt file it will be
out6654748541383250156.txt // it mean a randum number will be append with file
and you code in runable jar is no able to find these temp files
getClass().getResource() only reads resources that are on your classpath. The path that is passed to getResource() is, in fact, a path relative to any paths on your current classpath. This sounds a bit confusing, so I'll give an example:
If your classpath includes a directory C:\development\resources, you would be able to load any file under this directory using getResource(). For example, there is a file C:\development\resources\mp3\song.mp3. You could load this file by calling
getClass().getResource("mp3/song.mp3");
Bottom line: if you want to read files using getResource(), you will need those files to be on your classpath.
For loading from both privileged JARs and the file system, I have had to use two different mechanisms:
getClass().getClassLoader().getResource(path), and if that returns null,
new File(path).toURI().toURL();
You could turn this into a ResourceResolver strategy that uses the classpath method and one or more file methods (perhaps using different base paths).