I use new ClassPathResource("myFolder") to get some files from this folder "myFolder".
File file = new ClassPathResource("myFolder").getFile();
This returns file path my-project\build\eclipse\test\myFolder instead of my-project\build\test\myFolder
How can I fix it?
It seems that you have a classpath issue in eclipse. The result that you're getting is like that because eclipse does it's own voodoo when youre building and running the application.
Check your classpath in eclipse (and also in the Run Configurations).
Related
I'm trying to read a json file in a Resources directory and I use the following:
jsonObject = this.readJson(this.getClass().getClassLoader().getResource("jsonFileName").getPath());
In the IDE it runs correctly but when I build thw jar and try to run it by java -jar jarName I get a "File not found" Error and when I checked the path, it looks like this:
...projectName/target/projectName-1.0-SNAPSHOT.jar!/kb/is/identity.json
When running on the IDE the paths looks like this:
...projectName/target/classes/kb/is/identity.json
It's important to use getResourceAsStream, not getResource.
Have a look at How getClassLoader().getResourceAsStream() works in java
getResource("jsonFileName") - in this case root directory is project name using it under Idea, but when you run it under jar - I think that root path is User Home.
If I remember correctly, you can fix it with using / in resource path.
E.g. when using Maven, you have a resource directory. You have identity.json in the resource root. Using getClass().getResourceAsStream("/identity.json") receive this file (in Idea and in jar), because when you build a jar, all resources are copied to the root of the jar file.
I'm trying to export a java project in eclipse as a runnable jar, but for some reason the runnable jar doesn't work. If I double click the executable jar, it doesn't do anything. I tried both extract and package required libraries into generated jar.
So I also tried to export some simpler projects, those worked fine. The biggest difference is my real project has files: images and xml files.
In code reference them like this:
File file = new File("Recources/test.xml");
ImageIcon imageIcon = new ImageIcon("Recources/" + num + ".gif");
The structure of the project looks like this:
But in the executable jar they look like this:
Thank you for your help.
Edit:
I have tried the 'java -jar filename.jar', but now it says it can't find my resources folder, while in eclipse it can still find it.
Files in a JAR-File aren't just like files stored in your hard-disc. If you include files in a JAR, they'll be seen as a Stream of Bytes. So you have to use different methods to access these resources.
//To read/access your XML-File
BufferedReader read = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/test.xml")));
//To read/access your gif-Files
ImageIcon icon = new ImageIcon(this.getClass().getResource("/"+num+".gif"));
"/" is not the root-Folder of your file-system, but the root folder of the resources inside your JAR.
The issue may be that Java is not the default program to run the jar.
Try right click -> Open with, and select the Java Runtime, and it should run successfully.
Make it the default program to enable double-click running.
Right click -> Properties -> Change -> C:\Program Files\Java\jre7\bin\javaw.exe
Inspired by stratwine's answer at https://stackoverflow.com/a/8511277
So thank you all, but it seems like the problem wasn't the export only. There was an error I saw when I opened my program with cmd, I was using file name to open xml and images while I should have used inputStreams: https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html.
I am trying to switch my dev environment from Eclipse to IntelliJ. I am developing a Java EE application that uses the path returned from this.getClass().getResource("").getFile() to create a new FileInputStream. When running the project with Eclipse/Jetty (from the Jetty plugin on the Eclipse marketplace) and on production with Glassfish, the string returned looks like "/C:/path/to/class/in/jar/".
However, when running the project with IntelliJ/Jetty (8.* from http://download.eclipse.org/jetty/stable-8/dist/) the file path returned looks like "file:/C:/path/to/class/in/jar/". The 'file:' at the beginning of the string is causing a FileNotFoundException on "new FileInputStream(path)".
I am developing on Windows 8.
I can easily strip the "file:" from the string, but I would rather not change the codebase to fix what appears to be an environment issue. Is there something I can do so that getFile() does not return a path starting with "file:"?
Figured it out. The application was deploying one of the subprojects to a jar file instead of a folder. This was causing URL#getFile() to append the "file:" protocol for some reason. To solve this I went to Module Settings > Artifacts > 'exploded' module and moved the subproject's compile output from the jar file to a folder.
Yesterday, I had a problem because I couldn't manage to open a xml file (it owuld give me a FileNotFoundException) located in the ressources folder of my .jar file, which I managed to open on eclipse using the following lines of code. You can see my old problem here. This was my code with the problem :
File xmlFile = new File("ressources/emitter.xml");
ConfigurableEmitter emitter = ParticleIO.loadEmitter(xmlFile);
Someone told me it that one way was to use getClassLoader().getRessourceAsStream method to open a xml file in a .jar file that was exported
InputStream i= this.getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");
ConfigurableEmitter emitter = ParticleIO.loadEmitter(i);
Unfortunately, that solution only works when I export my project into a .jar file, so if I want to go back debugging my program, I have to take the old code that would only works on eclipse.
My question is: is there any better way to do this without having to change my code if I want to export it or if I want to debug it?
Thank you
edit :
Thank you all, it works perfectly fine now
my problem was that I put my ressources folder like that :
+project
+src
+ressources
+emitter.xml
InputStream i= this.getClass().getClassLoader().getResourceAsStream("/ressources/emitter.xml");
The above should work in both cases (Note is is /resources/.... This is assuming say your directory structure is below:
MyProject
+src
+ressources
emitter.xml
Place the file alongside your source files, then you can use the getResourceAsStream() method in both cases. Don't forget to update the path (which should be the package name of your class, but with slashes instead of dots).
My question is: is there any better way to do this without having to
change my code if I want to export it or if I want to debug it?
Yes, use Maven. Maven will handle that and it hooks into Eclipse beautifully (NetBeans too!) What you do is place the resource in src/main/resources and then you can have Eclipse run the test goal of the Maven project or you can just run mvn test from the command line. Another advantage of using Maven here is that you can also have src/test/resources/emitter.xml which overrides the one in src/main with environment-specific test instructions and it won't affect your deployment.
InputStream i= getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");
or
InputStream i= getClass().getResourceAsStream("/ressources/emitter.xml");
(note the absolute positioning)
both work when the class is in the same jar, on the same class path.
In the jar the names must be case sensitive, but as the jar already works. Ensure that the ressources directory is on the class path too, or copied to the target directory.
As "ressources" is probably configured yourself (not named "resources" as in English), you probably need to add it to the build somehow.
I 've developed an eclipse plugin and in that I have a java file trying to read directories and then populate result accordingly.
When I try to run the file from eclipse itself through Run>Java application , it gives me proper result but as soon as I try to run the same through Eclipse Application, it is throwing NullPointerException because unable to find the directory.
I tried the following ways-
Suppose , I have a package as -
Package - com.test.abhishek.file.java.TestWork.java
Directories - com.test.abhishek.file.java.Dir1
com.test.abhishek.file.java.Dir2
Now in TestWork.java-
InputStream is = HelpContentView.class.getResourceAsStream("/"+dirName);**
The above line is getting failed.
How should I keep my directory and where so that it will run as an eclipse plug-in as well.
Tried to find the class path by
TestWork.class.getClassLoader().getResource("").getPath() and getting the output as /
So now where should I dump my directories to ressolve.
Just trying to understand what you are doing. You have a directory within your source structure that you want to get? Eclipse plugins are normally placed in the /plugins directory jar'ed up. This means you either A) need to bundle your resources with your plugin using build.properties, or put them somewhere else in the file structure and access it using normal File IO mechanisms.
If you are creating a plugin you most likely want to use Bundle.getEntry instead
public void start(BundleContext context) {
Bundle bundle = context.getBundle();
InputStream in = bundle.getEntry("/"+dirName").openStream();
}
It looks like you need the findEntries API.
For some example code, check out the related question (and answer) at How to test if a URL from an Eclipse bundle is a directory?