private ConfigurableEmitter emitter;
File xmlFile = new File("ressources/emitter.xml");
emitter = ParticleIO.loadEmitter(xmlFile);
If I launch the project in eclipse, everything will works fine, but after I export my project and use JarSplice to create a .jar file, when I launch the jar file using the command prompt, the program will crash launching a FileNotFoundException, saying it cannot find the path specified.
java.io.FileNotFoundException: ressources\emitter.xml (The system cannot find the
path specified)
The surprising thing is that just before opening the xml file, I open a .png file located at the same place as the xml file, and this without any problem. In addition, when I open the .jar file I exported using winrar, I can find my xml file under the ressources folder. What can be the problem here?
Thank you!
EDIT :
Code with solution:
InputStream i=this.getClass().getClassLoader().
getResourceAsStream("ressources/test.xml");
emitter = ParticleIO.loadEmitter(i);
When you pack your project to JAR your resource don't live on disk, but are compressed into the JAR itself and you have to load as resource.There are a lot of guide on SO on how to load resource from JAR using ClassLoader.getResourceAsStrem() (follow this link)
Related
I have a problem to load a file from assets.
In pratcise I have to load TiledMap file and I do it in this way:
arrayTiledMap.add(new TmxMapLoader().load(Gdx.files.internal("scenario.tmx").path())));
(I add it in array for other reason)
In the project the tmx file(scenario.tmx) is located in the android assets folder.
When I execute the program in Eclipse there aren't problems , but when I create the JAR file for Desktop project and I execute it I get this error on console(I launch it by terminal):
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: Documents/University/Programming/Street.png (Internal)
The file Street.png is a file that I used in tiledMap editor for create the map.
At this point I check the JAR file contents and In the root directory of JAR there is the Documents folder and in it there is University folder and so on.
Because if the path is in the JAR file I get this error?
What is that wrong?
Thank you very much for your time, this error is driving me crazy
If you need other code, in particular, tell me.
(although I do not think since the problem only occurs when loading the file)
Because if the path is in the JAR file I get this error?
No. Its most likely you aren't exporting the JAR correctly.
File -> Export -> Java -> Runnable JAR File
And be sure to check this:
package required libraries into generated JAR.
So currently my netbeans project folders looks like this:
Block_Breaker <--Project
build
dist
Block_Breaker.jar
nbproject
src
packageONE
packageTWO
data.txt
manifest.mf
applet.policy
build.xml
I want to know how can i acces a data.txt file in packageTWO(when i run Block_Breaker through a jar file and not netbeans). Normally if run through netbeans the following code will work:
FileWriter x=new FileWriter("src/packageTWO/data.txt");
PrintWriter pr=new PrintWriter(x);
But if i run a jar file that netbeans created it doesnt work.
You can't write to that file once it is packaged into a jar file.
Yet reading is still possible using one of the following:
<YourClass>.class.getClassLoader().getResourceAsStream("packageTWO/data.txt");
// or
this.getClass().getResourceAsStream("/packageTWO/data.txt");
witch gives you an InputStream witch you can use to retrieve the content of the file.
If you are required to wite to that file then the simplest way is not to pack it into the jar but have it standalone some where on the filesystem.
More infos about getResourceAstream in the javadoc
This is because your .jar file does not include a folder named src/
Please use ClassLoader.getResource to load resources.
I put a file inside my Java project file and i want to read it but how can i find the path name with Java.
Here i put it in C driver but i just want to find path by just writing the name of file. Is there a function for it?
FileInputStream fstream1 = new FileInputStream("C:/en-GB.dic");
If the file is inside the jar file generated for your project (or in the classpath used by your project, generally), under the package com.foo.bar, you can load it using
SomeClassOfYourProject.class.getResourceAsStream("/com/foo/bar/en-GB.dic");
If it's not in the classpath, and you launch the application (using java.exe) from the directory c:\baz, and the file is under c:\baz\boom\, the you can load it using
new FileInputStream("boom/en-GB.dic");
Place it in your classpath, If it is a web application WEB-INF/classes/yourfile.ext, if it is a standalone application place it in bin directory of your application (default class directory is bin).
Then you could read by using one of the following ways.
InputStream in = this.getClass().getClassLoader().getResourceAsStream("yourfile.ext");
Or
InputStream in = this.getClass().getResourceAsStream("/yourfile.ext");
You can read online for the differences between above two approaches.
I have the problem running executable .jar file. I've created a project which contains a .properties file. It works just fine when I start it from eclipse, but when I export it to executable .jar file and try to run it with:
java -jar myfile.jar
I get the following exception:
(couldn't post image here)
http://imageshack.us/photo/my-images/824/29583616.png/
I've checked my manifest file in the .jar and it contains the
Class-Path: .
And here's the properties file loading:
properties = new Properties();
properties.load(new FileInputStream(
"src/com/resources/treeView.properties"));
Any idea what causes this exception?
If the properties file is inside the jar file, you cannot access it as a file.
You need to ask the classloader to get the resource as an inputstream. See Getting the inputstream from a classpath resource (XML file)
In Eclipse (and in most IDEs) the current directory is the project's root directory. This means that Class-Path: . means something else in Eclipse than when you run it from the command line. This is why you wrote "src/com/...". Remove "src":
properties.load(new FileInputStream("com/resources/treeView.properties"));
Your properties file is within JAR file. So, use : ClassLoader.getResourceAsStream().
I need to acces (create and read) a file from a JAR file (executable jar),
and that file should be created in the same directory as the JAR
I tried
this.getClass().getResource("myFile")
but since the jar has packages in it, it won't work..
I also tried write just
File f = new File("myFile");
f.createNewFile();
and that works if i execute the JAR from the terminal, but if i execute the JAR by double-clicking it, the file is created in my home directory -.-''
how do i access a file being SURE that that file is in the SAME directory as the JAR file?
(of course also getting the jar absolute path would do the trick since i can get the parent folder from it)
This will give you the full path to the Jar:
String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
EDIT: sorry, was in javascript mode when I wrote that :). As was so politely requested, in a static method you should be able to do this:
String path = Me.class.getProtectionDomain().getCodeSource().getLocation().getPath();
(where the class name is Me).