Unable to launch .jar file - java

I created a project that contains a JFrame and other JObjects. It also contains picture and other classes that have their own JFrame and JObjects, but I can't seem to get the .jar launching correctly.
I'm using DrJava and I'm not sure what I'm doing wrong. I clicked "create .jar from project", I put the main class in the text field, and it should create a .jar file. Except, when I look at the file, it's not a .jar but just a file.
If I change it to .jar it doesn't run. If I open it with WinRAR my classes and pictures and everything are in there. If I try to launch it in DrJava it immediately says "Execution of jar file terminated (exit value =1)".
As it's creating the .jar file, I get an error, it has all my classes under why and it says "Duplicate: (Insert one of my classes)$", but it says that DrJava was probably able to recover. After a second or two it says ".jar created successfully". If I try to launch it in CMD it doesn't open and I get a whole bunch of line errors, but if I compile it in DrJava it runs perfectly.
Why is this?

Because you didn't add a Main Class to run the program. I think you can add the main class using project properties in Dr.Java As in this Dr. Java

A file opened in DrJava probably has to be saved as a .java file. Its one of the many shortcomings of DrJava

Related

Creating an executable from a java project in eclipse

I have three classes, one of them is called Main and contains a main method, and no package. The IDE I use is eclipse. If I click File > Export and then select Java > Runable JAR File I'd expect to get a file that I can double-click and that'll run my program. However, that's not the case. If I click the resulting file nothing happens. I'm very new to this whole thing, so my questions are: Is this file even supposed to be clickable, or am I missing a step to run it? If that's the case, how to I convert the file to a .exe file (I really don't get this manifest-stuff, so please explain in detail how that needs to be done)? Basically: How do I create an executable (executable as in "works if i just click on it") file from three classes (with eclipse if possible)?
Edit: It's a console app.

Java/JavaFX, read files from inside project and jar file

I have some images and xml files inside my project folders which I want to read. For example, my code is inside src/app/MyClass.java. The files I want to read are in res/filename.png, res/filename.xml.
So if I wanted to give my project an icon, I would do this (works fine if running through Eclipse):
primaryStage.getIcons().add(new Image("file:res/icon.png"));
The problem with this is that when I run it through the .jar file, the image does not appear. Same thing with other files. It can't find the xml file through the jar file.
I tried this:
InputStream input = getClass().getResourceAsStream("res/icon.png");
primaryStage.getIcons().add(new Image(input));
But Eclipse says that input is null. What am I doing wrong? Any help would appreciated!
There are some possibilities as to why this is the case. The first of which is you are running a JAR and not an Executable Jar File. Another possible reason why the program fails to display images is that when exporting you are not clicking the extract required libraries button as shown below. If neither of these are the case comment below.
Try creating a source folder named resources (right click project > New > Source Folder) and put everything in there. You can now do getClass().getResourceAsStream("/file.ext") and it shouldn't be null.
If there are any other errors after you create your jar file (following the picture above), you can run this through command line/terminal and it should show you any errors you may have:
java -jar jarfile.jar

Running .jar File Java 8

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.

I can't run JAR file?

I have created a Java JAR file in NetBeans. I have already run clean and build on the program and everything was successful. When I run the program in NetBeans is also successful, but when i locate the JAR file in the NetBeans dist folder and run it, nothing happens. The program doesn't run, and there are no exceptions or messages. Could you please help me understand why.
EDIT: I already solved the problem.
It sounds like you're probably on Windows :)
One way to "run" a .jar file is to create a Windows File association for ".jar" suffix and the "javaw.exe" command:
1) Go into Windows Explorer and [Browse...] to your .jar file
2) Right-click the .jar file, and select the "open with..." option.
3) Select javaw.exe as above, and see if it runs.
Are you trying to make executable jar ? You might wanna put your main class in manifest of the jar file.
Something like :
Main-Class : file_name_with_main_method
I would suggest checking the Console if you are on a Mac. You can view errors from the Jar launch to understand why it failed.

why doesn't my jar file run outside netbeans?

I built a jar file that can run perfectly within netbeans when I click run,
but when I try to run the jar file by double clicking it it does not run, nothing happens..
Double-clicking the jar starts it, but unless you have a GUI application that opens a new window (in a different thread), it most likely finishes and closes before you can see anything.
In these cases you normally run the jar from the console (java -jar ..) to see if there are any exceptions/errors.
Starting an application from the command line would let you debug the problem as Bozho said. But you need to check if you have all the files in dist folder that you need to run your program. For example if you are using a database file to get the data, you need to place this file in the dist folder after program is built. I think the jar file is using a relative path when looking for files.

Categories