Running .jar File Java 8 - java

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.

Related

How to make my jar file executable by double click to open terminal

Okay so basically, every time I've seen this question asked it doesn't have the answer I'm looking for which is why I'm trying to ask myself.
Basically, I made a java project on BlueJ (required by school, sadly. But I can export it elsewhere if that's the problem (preferably IntelliJ or Visual Studio Code)), but essentially, I want to make it so I can simply double click my .jar export and it will open a terminal window and launch. When I try this however, I get this error: First Error
And when I click Ok then this appears:
Second Error
Now, if I want the jar to run on terminal, doing java -jar jarFileName.jar works perfect, but what I want is for that to happen when I double click the jar file; for it to launch and run from the terminal.
All the other answers I've seen for this problem blame the users computer and usually tell them to install some version of java or whatever, but this isn't the problem. I have other jar files that I can run perfectly fine, like Minecraft Forge installer or Minecraft Spigot installers (sorry only examples are Minecraft, not much else uses Java). And also this was for a uni project and no one else in my field know how to get their jar to be executable, nor does this file work for anyone else.
So yeah, I know it's a problem with how the jar was compiled or something and not what java is installed my PC.
I just want to make it double-clickable for the convenience of when I send it to friends, but if anyone knows another way I can get around this problem then that's fine. For example I know I could make a Bash file which simply does the java -jar jarFileName.jar for me or something, but I'd rather have it all as one jar file, so I don't know if this can be incorporated into the file but yeah.
Also I'd really like a solution which doesn't involve downloading external programs or whatever, since all I want to do is send this file to my friends and have them just double click it to launch.
TL;DR, what do I have to do to make my jar file actually executable by double clicking it. Thank you.
To make a jar file executable you have to set the entrypoint
You can do this by passing the e option to the jar command when you create the jar file, for example:
jar cvfe myapp.jar com.mycompany.myapp.MyApp com\mycompany\myapp
This will add the Main-Class entry in the jar's manifest pointing to the startup class, which should have a main method, for example:
package com.mycompany.myapp;
public class MyApp {
public static void main(String[] args) {
// start up the application
}
}
When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest file in an archive, and it always has the pathname
META-INF/MANIFEST.MF
The entrypoint is specified by the Main-Class, for example:
Manifest-Version: 1.0
Created-By: 1.8.13_37 (Oracle Corporation)
Main-Class: com.mycompany.myapp.MyApp
If you want to make an existing jar file executable you could unzip it, modify the manifest and zip it again (and rename it so it has the .jar extension - a jar is just a zip).
But I think the errors you show are not caused by the jar not being executable, but by some environment issue, causing the Java application to not find the necessary variables or paths to make the Java Native Integration (JNI) work. To solve this you would have to provide more detail on what you are trying to do with JNI.

Exporting processing to a jar file

I've been working on a processing application using ControlP5 and Twitter4j. I want to have my project run from a single jar file from any operating system. Basically I want to package up my application. My application uses images. I've been browsing for more than an hour, but I cant find how to do this. Any suggestions?
using
processing 2
twitter4j3
Thanks in advance!
I dont know if you can directly do it from the Processing IDE however, if export your sketch to a Java applet then locate the .java the the sketch folder you can use this in conjunction with Eclipse to export to a jar file.
So, I know that this post is very old but if you are still looking for a solution, or to other people that see this thread, it's relatively simple.
Export the project
In the folder with the exported project (something like application.windows64), navigate to lib and find core.jar and project name.jar (you need to have file name extensions visible)
Rename the files to .zip files
Extract core.jar to whatever folder
Extract project name.jar into the same folder (make sure you don't do it into a subfolder)
Click yes if it asks if it wants you to replace a file (if it doesn't you extracted the files incorrectly)
Delete core.jar and project name.jar
If the project uses images, move them into the same folder as all the other files
Select all of the files in the folder, right click, hover over send to and select compressed (zipped) folder
Rename the .zip file to name of project.jar
This might be old, but i still find other posts about it on processing forums.
This is the best way to run processing project as a jar file.
When exporting application, you will always end up with a lib folder inside exported application(whether for Linux and Windows). For windows, open command prompt(or power shell), you can use right-click+shift and then click on open power shell here.
After that you can run the following command.
java -classpath lib\* DisplayDepthStream
Now DisplayDepthStream is the name of sketch file.
To explain the command, -classpath lib\* tells java to add everything under lib directory to the class path. And DisplayDepthStream is the name of my main class.
Hope this helps~!
Chears

Having trouble compiling and recreating a JAR file using Eclipse

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.

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.

Java, running the code

I have a .java file that I created in eclipse. I managed to make a .jar out of it, but I don't know how to put my images folder into the jar, nor do I know how to make the jar run?
Should it run already, or would it just be an archive like a .zip?
Thank you guys so much for your help here, because I am completely and totally lost when it comes to this. I have been programming in java (just making .java files in JCreator basically) for about 2 years now.
I have searched and searched but never found a good answer. Anyone that can shed the like on how running .java files as an application and things work, that would be awesome.
P.S. I am using GUI, and the code is correct.
Thank you so much!
What is a runnable jar?
A jar is a archive (it uses the same file format of a ZIP) that contains program artifacts: compiled classes, images, configuration files and other resources.
To be a runnable jar it must contain a special file, called manifest (the manifest support electronic signing, version control, package sealing) with a Main-Class: project.name.EntryPoint entry that specifies the class that is the start point of the program.
This can be easily achieved by using the Eclipse feature File > Export > Java > Runnable JAR file.
How to run a runnable jar?
Using the command line, just do java -jar <jar_file_path>.
But nowadays the graphic shells of the operating systems (Windows Explorer / Gnome / KDE) already call java when you double-click a jar file.
How to put images on the .jar?
You can do this specifying on the Eclipse project that the images folder is also a source folder. Now when exporting the project to the jar file it will include them.

Categories