I have a project structure that looks like this:
Tester
\-- src
|-- hello.java
\-- vocabulary.csv
I load the csv file using getResource:
url = this.getClass().getResource("vocabulary.csv");
System.out.println(url.getPath());
new FileReader(url.getPath());
The program works completly fine until I export the project to a runnable jar file. Even though the URL is still valid (he prints the path instead of null) the console shows this:
jar:file:/home/malte/Desktop/vocs.jar!/main/vocabulary.csv
Exception in thread "main" java.io.FileNotFoundException: file:/home/malte/Desktop/vocs.jar!/main/vocabulary.csv (No such file or directory)
Can you explain me why this happens and how I can solv the issue?
P.S. I am using Xtend instead of pure Java
You need to use
InputStream is = this.getClass().getResourceAsStream();
once you package your source as a jar. You have just one file : your jar. Your vocabulary.csv is no longer a standalone file on filesystem anymore.
You can read more here.
Related
After reading about 50 different threads on the subject, I have not been able to have any of the solutions work.
What I'm trying to do is to access my config.properties file within the jar.
My project is a Maven project and the architecture is as follows:
My project
src/main/java
myApp
MyClass.java
src/main/resources
icon.jpg
config.properties
So when I try to access it from Eclipse it works perfectly good with this code:
InputStream configFile = MyClass.class.getResourceAsStream("config.properties");
Followed by more code here to retrieve properties...(that works)
However, when I export the project to a runnable JAR, it doesn't work any more. Inside the jar, files are as follows:
File.jar
MyApp
files.class here
etc...
META-INF
resources
icon.jpg
config.properties
Try this:
InputStream configFile = MyClass.class.getResourceAsStream("resources/config.properties");
Try the following:
InputStream input = AnyClass.class.getResourceAsStream("/config.properties");
It should work.
Hopefully this will make some sense. I'm new to Java and while I've built .jar artifacts before, I have never had to build a program that takes another file as input. I'm running into a weird problem and I'm not sure how to fix it.
Basically, I have a Main.java class that calls a .txt file and passes it to another class, myClass.java. I generated a .jar file with IntelliJ, which inserted itself inside its own folder.
To make things easier, here is the basic structure of my program.
/src
/com.myProgram
Main.java
input.txt
MyClass.java
/out
/artifacts
/myProgram_jar
myProgram.jar
And here is a basic version of what's in Main.java
myClass mc = new myClass("input.txt");
I tried running the .jar file with the command "java -jar myProgram.jar", and I get the error "The system cannot find the path specified". I ran getCanonicalPath() on input.txt. Apparently my program is looking for input.txt in out/artifacts/myProgram_jar instead of in the src/ folder.
What can I do? Even when I put the full path to force the program to look in the src/ folder, it will still attempt to look in the myProgram_jar folder and throw an error. So I cannot execute the file at all. Any suggestions for solving this error are greatly appreciated!
When you build to jar file, it's not a File anymore. A File is only a physical file on the file ystem.
So have 2 way to fix it:
1. You export the txt file to system as D:/New folder, then you get this file with path of this file.
2. You can try this:
try {
URL url = new URL(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().toString()+your path to file in structre);
InputStream in = url.openStream();
} catch (URISyntaxException ex) {
Logger.getLogger(PrepareGUI.class.getName()).log(Level.SEVERE, null, ex);
}
I have the following project structure:
ProjectName/src/java/com/main/Main.java
And I have a properties file in the following folder:
ProjectName/config/settings.properties
Now I tried to load the properties file in the Main.java with:
InputStream input = Main.class.getResourceAsStream("/config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
But this does not work. How is it the right way to do it?
Edit: I got the following error:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
Edit2: Now it works with eclipse. I did the following (adapted from getResourceAsStream() is returning null. Properties file is not loading)
1) This directory [config] must be put into the "build path". Right-click the directory in the Package Explorer or Project Explorer view, select "Build Path", then "Use as Source Folder". Note: This build path will be the class path for the project, when you run it.
2) As the config directory now is part of your class path and contains your properties file, you can simply load it with InputStream input = Server.class.getClassLoader().getResourceAsStream("settings.properties");
This works well for eclipse but not yet for a jar. I think I also have to add the build path somehow to the ant script.
How can I do that?
Edit3: If I take the settings.properties out of the config folder in the jar, then it works. Why? I want it in the config folder in the jar too.
Use following :
InputStream input = Main.class.getResourceAsStream("../../../../config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
By adding ../ I am getting 1 step backward in your filesystem. In your code you were using /config....., which means C:/config..... (if it is in C drive).
The following will also work in your case. (but not in zipped file)
InputStream input = new FileInputStream("config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
getResourceAsStream find files relatively compiled (class) files on in jar file. Example:
Your compiled files are in bin directory than you should put properties file to bin/config/settings.properties for give access to it.
You are using maven. So follow the maven structure and keep your resources like prop, xml etc files in resource folder and then call.
So folder structure would be like this
src/main/resource|
|
config|
|
settings.properties
InputStream input = Main.class.getClass().getResourceAsStream("/config/settings.properties");
Three things
You need add the settings.properties to a jar (can be the same jar as your Main)
Your classpath should include this jar
You need to use the context Class Loader to load the properties file in you Main.
Classloader cl = Thread.currentThread().getContextClassLoader();
cl.getResourceAsStream("settings.properties");
I really need your help to solve my own problem. Now, I'm dealing with small code app. In that project folder contain some resource files (*.xlsx, *.png,...). I placed them in current folder with code file. I just wonder that when I run my code in netbean ide, it just worked find.
After I build code project, I get a jar file in "dist" directory. I run it. It open normally since app used JFrame as user interface. However, when I execute some function of that app, it showed me the error log. Here is the error message:
java.io.FileNotFoundException:
src\sample.xlsx (The system cannot find the path specified)
What's the matter out there?
Here is some pieces of my code:
copyFile(new File("src\\sample.xlsx"),
new File(txtout.getText()+"\\sample.xlsx"));
Node: copyFile function is used for copy file from source to dest.
Here is my project folder structure in Netbean IDE:
Project Name
Source Pakage(src)
myClass.java, sample.xlsx, etc
First, never reference src directly, the directory will not exist once the program is built. Second, you can not access resources which have been embedded within in the application context via a File reference, they simply no longer exist on the file system.
Instead, you need to use Class#getResource or Class#getResourceAsStream
URL url = getClass().getResource("/sample.xlsx");
InputStream is = getClass().getResourceAsStream("/sample.xlsx");
// Don't forget to manage your streams appropriately...
Well you can create a folder named resources under the src folder put your resources in it and use them in your code by using getResourceAsStream() and getResource() methods that can access the embedded resources.Clean and Build will compile the code and embed the contents of the resources folder into the application’s .jar file.
Ways of Accessing resources :
String pathToImage = "resources/images/filling.png";
InputStream stream= ClassName.class.getResourceAsStream(pathToImage );
String pathToImage = "resources/images/filling.png";
InputStream stream= ClassName.class.getResource(pathToImage );
please refer the link information
I have a Java console applicaton with a resources folder. It works OK, but when I want to export my code to a runnable Jar file(with Eclipse), then the exported Jar can not find the files(they are in buildpath) and give a Filenotfound exception.
When I unzip the exported Jar file I can see the files are there in the root folder, so I guess something wrong with my code.
BufferedReader srcFile = new BufferedReader(new FileReader("resources/"+filename));
String line = srcFile.readLine();
I tried to use
URL url= ClassLoader.getSystemResource(filename);
filename=url.tostring();
But no luck.
Use getResourceAsStream(String) (docs) if you want to read in a resource on the classpath.