I exported an executable Jar file from Eclipse.
On Windows when I double-click the file, it executes properly. When I run it from command line with java -jar MyJar.jar it also works.
But when I upload that file on my linux VPS and try to run it there, I just get the error
Error: Could not find or load main class mypackage.MyClass
I tried many things like changing CLASSPATH, trying out different Class-Paths and Main-Classes in the MANIFEST.MF file, and even trying to execute the program from .class files without the jar container. But unfortunately nothing works.
Do you have any ideas? Or did you face the same problem?
EDIT:
Manifest:
Manifest-Version: 1.0
Class-Path: .
Main-Class: mypackage.MyClass
Name: about.html
SHA-256-Digest: 1fuYr0bu6NsJZCAmSom5QtnvnV0Cg8DwLn77Dd1Ql24=
Name: jfx8.cssext
SHA-256-Digest: b2p9PXeoxqnTdYgBFeKp3s7J9PajukOS3sGqyM4s36I=
I finally solved my problem!!
The problem was that no JavaFx runtime was installed on the VPS. Don't know why that caused that error. I rewrote the Application without JavaFx and now it works.
Thank you for all your help.
Related
I'm trying to make a jar that the user can double-click to run (not a .exe just a jar that can be double-clicked). The problem is while this question has been asked many times none of the answers have worked.
I think the problem is not on my system, because other jar files on my system (that I didn't create) run fine. This makes me suspect I am doing something wrong.
First I tried this example.
However when I tried running the jar I made with that solution with:
java -jar HelloWorld.jar
I got the error: "no main manifest attribute in HelloWorld.jar"
I suspect the error is with my manifest file so here is is:
Manifest-Version: 1.0
Main-Class: main.Main
I also tried: "File --> Export --> RunnableJar" in eclipse but while that allowed me run to my jar from the command line when I tried double-clicking it I got the following message: "A Java Exception has occurred".
At this point, I don't know what to do to make my jar double-clickable.
What can I do to make my jar double-clickable?
Update: I used a new command:
jar cfe HelloWorld.jar main main.class
Now when I try to run this new jar from the command line with
java -jar HelloWorld.jar
I get the following error: "Could not find or load main class main"
If you did everything as described in that example, I think that there is no association with the *.jar file in the registry. Check it.
Also it maybe that two versions of Java installed.
Check Right-Click -> Open With. Java Runtime should be listed there.
I have written a java application in eclipse and then I have made its runnable JAR file, but the problem is that the application is not opening when I click its JAR file to open it, but the application is opening and running perfectly when I open it in the eclipse, I have made the JAR file number of times again and again but I am facing the same issue every time, no error is shown when I click the JAR but it is not opening.
So please tell what can be the problem with the JAR file.
Any number of things can be wrong with a jar file that prevents you from being able to run it after you create it. Some things to check:
Do you have java associated with your jar file?
Did you specify a main class when you built your manifest?
Are there dependent library jars your application depends on that did not get added to the classpath? The manifest file in your jar file should tell you any additional classpath entries that were generated.
Do you get an exception when you run it from command line?
You can run it two ways:
java -cp [path to your jar] your.package.MainClass
java -jar [path to your jar]
See if any of these are your issue and it should point you to the solution to your problem.
When you generate a JAR file, it doesn't mean something is going to happen, maybe it is just a library you generated, a reusable module of the system, etc.
"JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform." - Wikipedia
So, Make sure you have a main class in your manifest, for example:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 16.3-b01 (Sun Microsystems Inc.)
Main-Class: com.example.MainClass
Class-Path: lib/lib1.jar lib/lib2.jar
When I try to open a .jar file by double-clicking it I get this message that "Could not find the Main Class", however it works when opening it with the command line.
I've been looking for a solution for some days, I found a lot of people asking the same questions, some of the answers I found suggest that the main class is not declared in the manifest.mf, however I checked it and it is declared, here is my manifest.mf
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_13-b20 (Oracle Corporation)
Class-Path:
Main-Class: my_pack.Mainprog
[empty line]
[empty line]
I tried to run the jar that NetBeans builds, also with the fat jar plugin for eclipse and always get the same error.
What I would do is:
Create a dummy project in your Netbeans and export it as a runnable jar. Then take your manifest and the dummy-project manifest and compare it.
Manifests are really a pain in the code. They need to be absolutely correct formatted. Take care on trailing whitespaces, missing/additional empty rows etc...
Maybe check the Java Version, you are using. Make sure, that your command line uses the same one, like setup in your environment variables. Maybe there are doubled declarations...
Which java.exe is associated with *.jar files in windows explorer? Try exactly THIS java.exe to run your jar from the CLI. Same issue then?
Good luck!
This is a very common question and there are chances that this might be marked as Duplicate, but even after reviewing a lot of answers and posts from stackoverflow and other communities, the problem just doesn't solve.
I have created a Project in my NetBeans IDE 7.3 and I Build the Project to get the JAR file by pressing SHIFT+F11. The next step, I perform is through WinSCP I copy the JAR file from my Windows Machine to the directory (/home/pi) in Raspberry PI.
Now, I am in the directory in my Raspberry PI where the JAR file is present and in the terminal I run the command
java -jar ProjectFinal.jar
I get the error "Could not find or load main class".
The contents of my JAR are as shown below (in the form of directories):
lib (contains all the libraries)
META-INF (contains a MANIFEST.MF file and the contents looks fine) and the contents are shown below:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_17-b02 (Oracle Corporation)
Class-Path: lib/super-csv-2.1.0.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: projectfinal.ProjectFinal**
and finally projectfinal (contains all the class files). I tried to change the contents of the MANIFEST.MF file by following this link. But this couldn't help me either.
Please suggest me any ideas that could get me out of this. It could be really helpful. I can share the code if it is needed.
I am using JRE version 7.
Any questions/inputs/ideas is deeply appreciated.
Please help.
Thanks a lot in advance.
I don't know about NetBeans but Eclipse IDE has an option to export into a "Runnable JAR File" instead of a normal JAR file.
I had the same issue and it solved my problem.
If not then follow this:
The text file must end with a new line or carriage return. The last
line will not be parsed properly if it does not end with a new line or
carriage return.
I've got a weird problem that I can't understand... I have a simple HelloWorld jar that I built in Eclipse which has the Apache Loggings jar on it's classpath. I've written a script to run the jar:
#!/bin/sh
export CLASSPATH=lib/*:$CLASSPATH
java -jar HelloWorld.jar
The directory structure here is a main directory with the HelloWorld.jar and a lib subdirectory holding the commons-logging-1.1.1.jar.
Running this script works fine. However, when I place the HelloWorld.jar into the lib directory (i.e. to contain all the JARs in one place), and executing java -jar lib/HelloWorld.jar, I get:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
error. Why?!?!?!?!
I'm asking this because I've used the JarBundler on OSX to create an Application bundle for the HelloWorld app and placed a modified script in the MacOS directory whereas all the libs are placed in the Resources/Java directory. Modified version of the script is:
#!/bin/sh
RESOURCE_DIR=$(cd "../Resources"; pwd)
export CLASSPATH=$RESOURCE_DIR/Java/*:$CLASSPATH
java -jar $RESOURCE_DIR/Java/HelloWorld.jar
and I'm getting the same error as above I'd really appreciate any help understanding why I can't do this and/or how to fix it?
Classpath doesn't work with wildcards. Every jar has to be specified explicitly, either as part of the CLASSPATH variable or in the manifest of another jar that is included in the classpath.
Also, IIRC java -jar ignores all the third party jars that are present in the classpath. Why not do this instead?
java -cp yourJar:logJars <mainClass>
Try to add the commons-logging-1.1.1.jar to the CLASSPATH directly
Java will not work with lib/* but the shell may be expanding it for you. Double check this. Put a line like this after export:
echo $CLASSPATH
Also, I would recommend putting it in the MANIFEST file as already mentioned.
EDIT:
Is it a permission problem? If you run the app as root/admin or put the file somewhere else and use a fully-qualified path does it work?
Use the MANIFEST file (META-INF folder) to deal with Classpath entries. Use relative paths for the libraries.
For further info, take a look here.
Basically, for the case with commons-logging inside a lib folder:
Class-Path: lib/commons-logging-1.1.1.jar
And for both jars in the same folder:
Class-Path: commons-logging-1.1.1.jar
Thanks to everyone for their help in figuring this out. Basically, a manifest file was being created and bundled into the jar without my knowledge so any $CLASSPATH or -cp flags were being ignored. In my Eclipse project, I had my classpath set to $(projectRoot)/lib which coincidentally the same directory structure as my dist directory. However, when they were bundled into one directory by OSX's JarBundler, the directory was no long present, hence the classpath errors!
I tried removing the Class-Path attribute from the MANIFEST.MF that Eclispe created but the command line $CLASSPATH and/or -cp entries still don't seem to make a difference... Does the existence of a manifest file negate all command line classpath entries?
have you set log4j.jar into your class. i think you didnt added log4j.jar of to its class path.