java - Sharing an executable .jar file - java

I wrote a program in NetBeans, and now want to share it with my coworker. However, when he tries running it on his computer, he gets an error message:
"Could not find the main class: excelcomparator.ExcelComparator. Program will exit."
Here's the confusing part: I wrote this on my laptop which has NetBeans, and to make sure that it worked, copied the dist folder onto a flash drive, and ran it on my computer. It worked fine. When I emailed it to my coworker, he got that error.
Based on what I've seen, some solutions are to run the .jar from the command line. While that might work, I need the file to be double-clickable.
I sent my coworker the file via email in a zipped folder, is it possible that unzipping the entire folder also messed up the .jar file (don't see why it should, but included anything that might help)?
This is the MANIFEST.mf that's within the .jar file. There is a carriage return at the end of the file, it just doesn't copy well into this text box:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.7.0_71-b14 (Oracle Corporation)
Class-Path: lib/poi-3.12-20150511.jar lib/poi-examples-3.12-20150511.j
ar lib/poi-excelant-3.12-20150511.jar lib/poi-ooxml-3.12-20150511.jar
lib/poi-ooxml-schemas-3.12-20150511.jar lib/poi-scratchpad-3.12-2015
0511.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: excelcomparator.ExcelComparator
I sent the dist folder, which includes the lib folder which contains all the additional .jars.
If there's any other relevant information I need to include, let me know. Thanks!

If you want the JAR to be double-clickable then you should bundle all the dependencies into a single JAR file.
You achieve this using the maven assembly plugin: http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
If you don't know how to use Maven, Eclipse has a nice wizard. I'm not sure how it works on NetBeans but the same question has been asked many times, e.g. here Netbeans Export to Jar, include all library files

Just to finish this off, I'll write what ended up working for me:
As per Franz Becker's suggestion based on this question, I bundled the entire program into a single .jar file.
I was still getting the same error on my coworkers computer, so based on other suggestions I saw, I guessed that the problem was based on different versions of Java installed on our computers. What I did was recompile the program to an earlier version of Java, which works with any version from then and onward (other people suggested reinstalling Java on the users computer. That's a bit too unrealistic in most scenarios).
To change the compliance level (in Eclipse): right click on the project in Package Explorer, select properties. Choose Java Compiler in the options on the left. Uncheck 'Use compliance from execution environment [default target JavaSE] on the 'Java Build Path'', and change 'Compiler compliance level' to your level of choice (I picked the lowest option available, 1.3, as I assumed anything higher than that wouldn't be an issue).
I then exported the file (File -> Properties -> Java -> Runnable JAR File), and had a working, clickable .jar.
Thanks to everyone for their help!

Related

Generating executable file in IntelliJ for Windows

I used to work on an IntelliJ project that was started before I started working on it. This project had a configuration that allowed me to generate an EXE file that could easily be sent to windows users.
It would generate a massive EXE file bundled with all the needed JARs of the application, and upon running it the first time, it would silently "install" itself into the AppData folder, as if it were a regular windows setup file, even though the user would not even notice it doing that installation.
I am trying to configure a project in IntelliJ to do the exact same thing, but first I have not been able to output the exe file by selecting "exe" as the Java FX native bundle type. It just generates a .jnlp and .jar file. When I select "all", as per some other post here in stackoverflow, it generates an exe file, but only with a few kb in size, which does not contain any of the .jar files that should be part of it. Moreover, when I try to open it, it just crashes saying the main class was not found.
Am I missing some setting for building the project? I am using IntelliJ 2018.3.2
Here are some screenshots:
I have been able to get past this error, after changing the verbosity of the compilation and going through the logs.
There was a log message which helped fixing this:
The process complained about the Inno Setup Compiler missing, which was right. Makes me wonder why IntelliJ would have that feature built in if it depends on external tools but does not notify the user clearly of this.
Detected [iscc.exe] version 0.0 but version 5.0 is required.
After going to the Inno Setup site, downloading and installing the tool, I was able to get the executable to be generated.
Now I am struggling with another error, which is the executable complaining about the main class referenced in the Artifact not being found, but at least I have moved past the first problem! Going to tackle this one now...
The best solution for this is using exe generator software.
There is plenty of exe generators out there.
EXE4J is the most simple & easy tool to use.
In EXE4J,
You can upload your main jar file and select the main class.
I think this will be solved your problem.
This may be due to you`ve extracted Jars to your output root, while you neet to Put it(you can check difference by deleting everything from your output root in Output Layout screen and then just right click on jar on the right side, you will see two options here, try another one

JFreeChart Java JAR not running on a different machine

I have created an application using the JFreeChart library for use on another machine.
Previously I have developed applications with JFreeChart (using the same libraries) which has worked fine on other machines. The only difference is this machine is running Vista.
Please see below for the run-time exception I am getting:
The class that cannot be found, however, is located in the highlighted jar in the below image showing my imported libraries for the JAR. I have also established that this JAR is included in the manifest for the application. See below image:
So I very much need this to work and have no idea where to look next - or what is causing this problem!
Development machine Java version:
1.7.0_45
Target machine Java version:
1.7.0_45
Thanks in advance.
Check the Class-Path attribute in your JAR's manifest, which should contain entries like this:
Class-Path: lib/jfreechart-1.0.17.jar lib/jcommon-1.0.21.jar …
Also, examine dist/README.TXT in your NetBeans project folder, which should say something like this regarding libraries required by your project:
To run the project from the command line, go to the dist folder and
type the following:
java -jar "CISOnlineMonitor.jar"
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
This has nothing to do with os i belive.You dont have all the necessery libs within your jar.Try to open a jar and see if you have them in.Fact that you are able to run it on your maschine only proves that.Make executable jar with eclipse or whathever you use.And when it ask you for libs check -Extract required libraries into generated Jar.
if you dont know how to get to that point
File>Export>Java>Runnable Jar File> Runnable JAR File Specification.
Also right click on your project and check Your build path.
RightClick project>Properties>Java Build Path>Libraries
Make sure you have everything correct
EDIT-
As i see you use NetBeans im not sure exactly how to find all this there.Bud it will be very similiar.

Exporting eclipse project to jar

I did check the already answeared questions which have almost the same topic as this question but none of the answeared onces were able to solve my problem.
I have been searching now online for about 4 hours and tried soo many different things to solve my problem..
Im trying to make a Pong game in Java and now i wanted to export my project so i can send it to some friend so he can try it.
Im using LWJGL for this project so i do have some jars added to the referenced libraries, And the LWJGL.jar has the windows natives added which is included in the projects lib folder.
Here is an image of the project viewer:
Reason im posting this image is so you get an idea of that i have all the libraries inside the project aswell as the natives needed for the project to run.
While reading about why the jar didnt work i also read that you need a Manifest.mf file so i created one and inside this manifest file i have the following text:
Manifest-Version: 1.0
Main-Class: jGame.Main
Class-Path: lib/jars/asm-debug-all.jar lib/jars/jinput.jar lib/jars/lwjgl.jar lib/jars/izma.jar lib/jars/slick.jar
Also 2 empty lines underneath Class-Path since i heard u have to have that.
This project runs fine aslong as i run it in eclipse but when i export the project with either runnable jar or jar wizard in eclipse it doesn't start when i double click the .jar file so i went into the cmd and used this command:
java.exe -jar JGame.jar
And the following message was shown:
I have checked atleast 10 times that i provided the correct path for the main class.. And i dont know what to do anymore. In the eclipse wizard i have been trying both to export runnable jar and exporting normal jar but none of the two is working. Also in the wizards i have selected to export a normal jar not runnable since when i choose runnable the libraries get messed up because the lib folder gets replaced.
I tried opening the jar file as an archive and got the following:
In an attempt of using JarSplice i suceeded to solve the problem.
After reading this:
It's not going to work the way you're trying to currently do it, since you need to have the native files along side the jar and point
to them via the '-Djava.library.path' parameter.
If you just want a single jar and want to avoid the hassle of the
command line and native files use the JarSplice tool. JarSplice is
easy to use and will automatically handle the native file stuff for
you.
1) Simply export your project (class and resources) to a jar (easier
just to do it through your IDE).
2) Then run JarSplice, add all the jars you need to the jars tab (your
app jar, lwjgl.jar, and any other external jar you're using).
3)Then on the natives tab add all the natives files (windows *.dll,
linux *.so, mac *.dylib & *.jnilib).
4)On the class tab add your main class. Then create your jar.
You can then run this jar just by double clicking it (or if you wish
via command line using 'java -jar yourapp.jar').
I found this solution from this link: Can't start .jar file (using LWJGL)
Reason why it didnt work the first time were i didn't include the actual .jar file that was exported from eclipse while using JarSplice.

Simple swing application. The jar file runs on my computer, but not others

This is my first question, so apologies for any mistakes. I'll try and give all the info I can. Basically I've written a simple swing application which just loads in image into a JPanel and displays it. I'm using Netbeans 7.1 and I have the latest version of the Java SDK.
Anyway, I've used the "Build" feature in NetBeans 7.1 to deploy my application into a Jar file. When I double click the Jar File on my PC, it runs without a problem. However when I put it on other computers (Tested on 2 others so far, both with the most current JRE) it fails to open, citing the following error:
could not find the main class: swong.Startup. Program will exit
swong is my package name, and Startup is the location of my main method. I have checked the manifest file which is created with Netbeans' build, and it[the manifest] does indeed contain the location of my main method class.
From searching, I've come across similar issues in which the classpath is set wrongly, but I don't understand how this could cause my particular error.
If someone could help me, I would be over the moon. I've been studying java for a year or so, and I've got a good handle down, but I've NEVER been able to make a Jar that runs on a computer which wasn't my own. So, 10 points and thanks in advance.
xo.
EDIT: Thank you for the responses. I'm doing shift work and swamped, but I will test and poke with these responses tomorrow and provide more information. Thanks again. xo
I had d same problem while distributing my app. There is 1 solution that you create a batch file with 'java -jar AppName.jar' and asking user to double click on this batch file to execute your app. What i did was to provide a JRE installation exe(eg: jre_1.7.0) with your app.
Now create a Batch file (install.bat) in which write following commands
jre_1.7.0 -> this will install jre on user's pc
set path="C\Program Files\Java\jre_1.7.0\bin"
java -jar yourAppName.jar
Why i installed JRE because different people have different JRE versions installed. So this makes it difficult to set path to the installed JRE's bin folder & calling the 'java -jar' command. Hence as you know which folders your JRE installation will create hence it is easy to set path and execute your jar file with 'java-jar' command.
Check that your jar file has the following structure (at least)
jarfile.jar
|---------- swong
|---------- Startup.class
|---------- META-INF
|---------- MANIFEST.MF
It seems like the class "Startup" is missing. Maybe your jar only contains the .java files, not the compiled classes.
This error message can be a mistakable java7 error, when you try to start java7 compiled classes with a different Java Runtime Environment then java7. Have you validated, that your .jar is started within a Java7 environment on those other test machines? Sometimes it happens, that you have installed different versions of JREs and you might not be sure which one is actually started.
To check which enviroment is used, you can check in your registry for the following value:
HKEY_CLASSES_ROOT\jarfile\shell\open\command
this should point to your latest JRE. Or if you'd like to stay compatible to java6 as well, define the appropiate compile level in your build environment.

Create java application jar file in eclipse

I have created a java application in eclipse, wich needs comm.jar and jexcel.jar and .property files so i have added to libray. I want to make a jar file out of my java appliction, including the external jar files added to the appliction.
How can I do it? To run serialport programs I have copied win32.dll into java_home/bin and comm.jar into java_home/jre/lib and javax.comm.properties into java_home/jre/lib, but when delivering the product it should run only by needing the jre.
How can I solve this? Please help me.
Thanks in advance,
suma
Although your question is not totally clear I suggest using the Fat Jar Plugin should allow you to achieve what you want.
You can use File > Export > Executable Jar which includes all libraries. There is also a checkbox to generate an ant build file as well as the jar in order to customize it further (I for instance make all the paths relative and remove the main-class flag).
You have two "path" issues. The Java Classpath and the path from which dlls are loaded.
If you were using a Java EE app server or OSGi then controlling these paths is addressed by the respective runtimes. Both Java EE and OSGi are likely to be overkill for small projects.
In which case you are delivering:
Your application JAR
The dependent jars
The DLLs
I suggest that on installing your app you place these artefacts into a suitable directory structure, for example .../myapp/lib for the jars and .../myapp/bin for the dlls. Don't copy them into the infrastructure directories, for example the JRE lib and bin, or into Windows32 - that just leads to version nightmares and mysteries when someone installs a new jre.
Having got that structure, how to control the paths? For the classpath, look at the Manifest.mf file. tutorial
For the DLL path, I know of no good alternative to setting OS level environment variables ** before ** launching the JVM. Hence you need a little batch/shell script to launch your app, setting the PATH appropriately.
You can also check maven.
You can right click on the project and say "Export". Now select "Java" in tree of choices. Under that select "Jar File". It'll guide you through the process and will allow you to export you project as a jar file.
Hope thats what you are looking for.
The recently released Eclipse 3.5 has a Export as runnable Jar which allows to put all dependent jars in a subfolder to the jar file, and get the Manifest right.
It is an adaption of the FatJar plugin. Works nicely!

Categories