This is a really common error, because there are tons of threads about it, but I'm not sure if since my situation is slightly different from all of them the solutions don't work?
Basically, I'm in eclipse. I have a source folder called src, then I have a package that goes down three folders, then the class in question. The class uses the code:
BufferedImage im = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(filenames[x].concat(extension)));
surrounded by a try/catch. filenames is an array of all the file names I am loading (this code is run multiple times in a for loop) and extension is ".PNG". The pictures are located in another source folder called EngineTextures.
Running this program works fine in Eclipse! All textures are loaded and all my other code runs! However, I export it as a runnable jar and run it in command prompt to recieve input == null errors on all of them, pointing to the line that has ImageIO.read(Thread.currentThread() in it.
The kicker is this whole thing worked in a separate project before, and when I even tried re-exporting that project, I recieve the same errors on completely unchanged code. This leads me to believe I have some obscure Eclipse setting changed wrongly.
Opening the jar, my MANIFEST.MF has a version of 1.0 and a classpath of just plain ., which I thought was correct for this kind of thing? The Main-Class points to the right place, and all my pictures are right there next to the META-INF folder.
Solutions I've looked at unsuccessfully:
getResourceAsStream working in eclipse, but not when run as applet in browser
Why does getResourceAsStream() work in the IDE but not the JAR?
Java IDE - Eclipse, Importing resources
Audio file in jar made by Eclipse IDE
getResourceAsStream() returning null in jar but fine in eclipse
Additionally, I completely deleted the workspace and recopied my pictures and code into the same state, thinking maybe some .metadata thing was wrong, to no avail.
Thank you, in advance, for any and all help. I hate to make a repeat like this but no solutions have worked thus far. Please let me know if I have not given any crucial information.
Opening the jar, my MANIFEST.MF has a version of 1.0 and a classpath of just plain ., which I thought was correct for this kind of thing?
No. The Class-Path entry in a JAR file names other JAR files, relative to the location of this jar file. It doesn't name directories:
"The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs."
That in turn implies that resources to be loaded via getResourceAsStream() must be in JAR files.
Related
I've currently finished my project, but can't get it to work when it is exported. I use JAXB to read and write XML Files and also have dependencies on other external Folders, which are needed to use a POS-Printer.
I've managed to link my external XML Save-Files with absolute paths, but not with relative paths. So that worked, although not the way i wanted. Yet, using the external class folder for the printer didn't work at all.
This means, that in my Eclipse Project Build Path i've added a class folder, which contains all of these needed files (which are not only jars, so adding them one by one wouldnt work). So exporting my project to a jar either includes all the files into the jar itself, or doesnt include them at all.
Everything works perfectly in Eclipse, but not when i export it.
My folder structure looks like this:
src
/model
/view
/control
data
/articles.xml
/...
JavaPOS <--- needed folder with all its files
/jpos.xml
/xerxers.jar
/swt-..-.dll
I've tried:
InputStreams is = getClass().getResourceAsStream(url);
absolute paths
manipulating the manifest file and/or jar structure
runnable and non runnable jars with nearly every combination of options
putting the files inside the library "by hand"
changing the build path of the project
My Question is:
How do i get my jar-file to know where these files are?
EDIT:
Do you think Maven or an Ant file could solve my problems? I don't have any experience with those.
The Problem was, that i had more than one JRE installed and that the one eclipse was using, had all the dll files, but the other ones didnt have it. So i had to add them manually, because reinstalling the drivers of the printer didnt change anything. Gotta fix that somehow, but right now it works and that is all i wanted.
Turns out i didn't even need that Folder, just needed one file out of it and the missing dlls.
I've run .jar files before, but I've encountered a "different" situation, and I'm not sure what to do. I'd appreciate if someone could help me out.
Previously, I programmed with Java 6 and Eclipse Juno exported all my programs to runnable jar files. I'd get a .jar file that I could run by just double clicking on it. The files always looked something like this (note the jar file icon):
Recently, I wrote a program in Java 8 with Eclipse Luna (Release 4.4.0) and exported it to a runnable jar file, and I got something different (note the different file icon):
It no longer runs when I double click it. Instead, my computer uncompresses the jar, as it would a zip file. I tried running it from terminal. I cd'd to the directory and typed
java -jar graph3D.jar
I got the following error message:
Error: Unable to access jarfile graph3D.jar
After uncompressing the jar file, I found a folder named META-INF with the manifest file, MANIFEST.MF in it. It was the only file that seemed to resemble an executable file. Do I have to do something with that?
Could someone explain how I can run the second jar file graph3D.jar? Is it something new with Java 8, or something different about Eclipse Luna, or something else?
(Both programs run fine in Eclipse, by the way)
Thanks for your time and help.
Edit:
Below was the dialog box Eclipse displayed if anyone is interested.
Selecting "Use .jar;.zip" makes the filename "graph3D.jar;.jar;*.zip" .
Selecting "Use .zip" makes the filename "graph3D.jar;*.zip"
Selecting "Cancel" doesn't let you go forward.
You'd have to manually delete the extra file extension.
Somehow when you exported the file, the filters for the file dialog box (*.jar;*.zip) got attached to the filename, which is graph3D.jar;*.jar;*.zip, not graph3D.jar. Java can't find it because it doesn't have the name you supplied. Rename the file and pay close attention next time you export; either you fat-fingered something, or you're triggering a significant bug that needs fixing.
I recommend that you will access the build folder after you've built your project on the IDE under your project folder (in your workspace) and copy both the libraries folder and the .jar and post them wherever you want the program to be "installed", you'll then have an executable jar that should run smoothly without problems, just as I said don't forget the lib folder.
I think there is nothing new in Java 8 related with the running jar, I guess you need to check the the Eclipse export issues, it seems your classes are missing from your second jar file.
(4/15/2014 Still no working answer to the question)
I used gdx-setup-ui.jar to create my Android/Desktop/HTML5 program which I imported into Eclipse.
myprogram
myprogram-android
myprogram-desktop
myprogram-html
My program runs fine on desktop and android, but when I run it as html I get an error if all of my classes are not in the same myprogram>src folder, if I put it in a sub folder in src then the html5 does not seem to access the class. If I take my classes out of the folder and put them in the root of myprogram>src they work. How can I fix this?
I also notice that when I do a symbolic link to the asset folder manually(without gdx-setup-ui.jar) my Java application will not find the pictures etc unless I physically copy the asset folder twice. I made sure the folder path was exactly the same and it still wouldn't detect it unless I made a second copy of the pictures. Strangely the gdx-setup-ui.jar does this symbolic copy and yet it works for some reason.
This tutorial explains how to do it in Eclipse. And for completeness sake, here is how to do it using ant and IntelliJ. I hope this was useful. I will try the Eclipse one now...
I have a Java JAR file that was developed to run in the browser. It works fine. There doesn't appear to be any sort of build file associated with the source code I've been given and I'm attempting to create a project and/or build script.
The source code references some third party code. What I've done is unzipped the JAR file into a folder that has this structure:
\App\src\com
\App\src\META-INF
\App\src\applet
The "com" folder contains subfolders which contain the .CLASS files for the third party libraries. "applet" contains the .JAVA source code files (one folder, maybe 15 files).
The code appears to be written in 1.4 syntax (I manually attempted to compile some of the .JAVA files and it complained that some things were deprecated and/or required the older version).
I created a Java Project in Eclipse and pointed it to the \App folder. Then I went into the Properties for the project and went to Libraries and and clicked on "Add Class Folder" and added the \App\src\com folder. Then I went to "Order and Export" and moved this to the top. I also set the Java Compiler version to 1.4.
I am getting a large number of compiler errors, all of which seem to stem from the fact that it says it can't resolve an import. This import is the set of .CLASS files contained in the "com" subfolders. The namespace matches the directory structure.
This seems like it should be really straightforward, but I've tried various things and don't seem to be making any progress. Based on what I've read I'm sure it has something to do with the pathing but I'm at a loss at this point on how to fix it.
(I'm sure it goes without saying that I'm not a Java dev.)
Any thoughts or ideas as to where I'm going wrong?
Can you please try to right click on Eclipse's Navigator. Select Import>General>archive file> (Jar file that you want to import).
You should be getting the jar file correctly imported at this point. Do tell me if you face any problems.
In eclipse I have a java project that runs an external program (a .exe) from the main method. This program is in a folder ext under the rot of the project on the same level as the source folder. Now when I run this program from Eclipse it works Like a charm. But when I export a jar file, it does not. I end up with a jar file that does contain the ext folder, but I still get a FileNotFoundException.
I did think of using getClass.getResource('path'), but I can't use this from the static main method.
Any ideas?
To get around the static issue, you can use ClassLoader.getSystemClassLoader().getResource('path')
This may or may not work depending on how you are launching your .exe (it doesn't really have a file path now, since it is inside the .jar, and I'm guessing it won't) - having a .exe inside a .jar seems odd to me. Why not leave it outside the .jar, and specify its location with a configuration parameter? This would make much more sense.
Try
Foo.class.getResource("...");