fail to execute jar file in linux - java

I had developed a standalone program which don't have any GUI, it's only use in the linux machine(without any GUI). So, i converted my java source code into jar file using the following command
jar cfm hardcoded.jar manifest.txt hardcoded.class
The jar file was successfully created but when i try to execute the jar file in the terminal,i get this error
no main manifest attribute, in hardcoded.jar
Some of the information said that the problem due to the manifest file but i can't figure out where is the root cause because i am very new to java on linux,Some people said that the package also need to include but where can i find my package name?.My manifest file is showed as the following
Manifest-Version: 1.0
Class-Path: ./ commons-logging-1.1.2.jar httpclient-4.5.2.jar htt
pcore-4.4.1.jar java-json.jar java-rt-jar-stubs-1.5.0.jar javax-ssl-1
_1.jar joda-time-2.2.jar
Class-Path: .
Main-Class: hardcoded
As you can see my manifest file, i had some others external library, i know eclipse have the build in function to solve this issue, how i need to solve it in linux environment ?

Related

jar file works in eclipse folder but not out of it.Main class notfound

My groovy (ant1.8.1 for build)project was build in jdk1.5 and now i am making some changes and upgrading to jdk1.8 .
When i build and make jar ,it works fine from the folder where code is in eclipse.
but when copy that jar and try to run get below-
Error :could not find or load main class Run.
Rest all jars are as below(same as used in jdk5)
groovy-all-1.6.9.jar
commons-codec-1.4.jar
commons-httpclient-3.0-rc4.jar
commons-logging-1.1.1.jar
commons-net-2.0.jar
lib/log4j-1.2.15.jar
jtds-1.2.5.jar
httpmime-4.0.1.jar
When you say "run", I understand you mean something like:
java -jar MyJar.jar
You should check your MANIFEST file inside the JAR and make sure your Main-Class is defined as you need.
Main-Class: pakage.name.ClassName.class
And the corresponding pakage.name.ClassName.class exists in your jar file (or is included in any of the libraries accessible through classpath)
Check this anyway Setting application entry point

java.lang.NoClassDefFoundError - Generating Executable Jar

I have created my JAR on Windows 2000 having java version 1.5 which contains following directories/files:
manifest.txt
com
lib
lib contains all JARS which I want to make part of my JAR. com contains my class files and below is manfiest.txt file
Main-Class: com.as.qst.result.ResultTriggerSchedular
Class-Path: lib/axis.jar lib/c3p0-0.9.1.1.jar lib/commons-discovery-0.2.jar lib/commons-logging-1.0.4.jar lib/jaxrpc.jar lib/log4j-1.2.16.jar lib/medplus-hub-8.2-wsclients.jar lib/medplus-hub-13.1-jaxws-clients.jar lib/quartz-2.2.1.jar lib/quartz-jobs-2.2.1.jar lib/saaj.jar lib/slf4j-api-1.6.6.jar lib/slf4j-log4j12-1.6.6.jar lib/ wsdl4j-1.5.1.jar lib/xercesImpl.jar com\as\qst\result
I used following command to generate my JAR
jar cvfm test.jar manifest.txt com lib
It has successfully created a JAR file but when I try to run it with
java -jar test.jar
it does not execute and throws above exception. I used the same process for Windows 7 which has version 1.7 and it did work out even without class files path in manifest.txt com\as\qst\result. Is something more to do with class-path besides defining in manifest? and why is it working in Windows 7?
You do not need the class file path in your class path entry. So instead of adding com\as\qst\result to your class-path.
More over you must not package other jar files in your runnable jar.
Other required jars must be provided in the same folder as your jar file (may be in separate folder) and Add current directory "." (without quotes) to your class-path.
Hope this helps.
EDIT
Just found this Stackoverflow Link. This might give you more insight. Please read through it.

Receiving a NoClassDefFoundError (Eclipse generated jar) using Apache procrun but runs fine on command line

I have exported my application as one large package from Eclipse so it uses the Eclipse loader. I have installed it with procrun using an install.bat file, the most important lines are shown below:
set PR_CLASSPATH=C:\somepath\ECGIServer.jar
set PR_LIBRARYPATH=C:\somepath\native\lunaLibrary.dll
set PR_STARTMODE=jvm
set PR_STOPMODE=jvm
Obviously there is much more and the service does run. In the ECGIServer.jar file, here is the Manifest file:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ commons-codec-1.6.jar stax-api-1.0-2.jar LunaProvider.jar
Class-Path: .
Rsrc-Main-Class: ecgi.encrypter.ECgiServer
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
There are more jar's to list but too many to write. Regardless, when I run it (ECGIServer.jar) at the command line it works great. When I run it as a service, it runs until I try to create an instance of LunaProvider. Once the application creates an instance of LunaProvider I get:
java.lang.NoClassDefFoundError: com/safenetinc/luna/provider/LunaProvider
As you can see, LunaProvider is a ThridParty library that's just being pulled in from my Eclipse setup.
Well finally decided to try exporting dependent directories from Eclipse to a separate directory. After I did this all my dependencies, including LunaProvider.jar got dumped into that directory. I changed the following line in the install.bat file to:
set PR_CLASSPATH=C:\somepath\ECGIServer.jar;C:\somepath\ECGIServer_lib
And everything worked out just fine. Still can't figure out why.
The way I figured it out to make it work, was generating jar and the library folder apart. This generates .jar file and a directory called YourAppName_lib.
So in the .bat you have to set PR_LIBRARYPATH to the _lib folder and that´s it.

java.lang.NoClassDefFoundError thrown when running jar file

I am trying to run my jar file on a Mac (Haven't yet tried on any other platform). When I run using java -jar MyApp.jar I get the following error
Stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at tabalchi.MyLogger.getLogger(MyLogger.java:51)
at tabalchiApp.TabalchiApp.printSystemProps(TabalchiApp.java:117)
at tabalchiApp.TabalchiApp.main(TabalchiApp.java:37)
So, as you can see the main class is recognized.
This is the manifest file.
--->
Manifest-Version: 1.0
Class-Path: . jars/log4j-1.2.16.jar jars/jfugue-4.1.0-20120125.jar jars/gervill.jar jars/AppleJavaExtensions.jar
Main-Class: tabalchiApp.TabalchiApp
SplashScreen-Image: tabalchiApp/resources/splash.png
<---
And the folder structure in the jar file as follows.
--->
META-INF/MANIFEST.MF
jars/AppleJavaExtensions.jar
jars/gervill.jar
jars/jfugue-4.1.0-20120125.jar
jars/log4j-1.2.16.jar
tabalchiApp/TabalchiApp.class <-- this is the main class
tabalchiApp/otherClasses.....class
<---
I have read many posts about running .jar file but none of the solutions have worked for me. I would really appreciate your help on this one.
I am creating the jar file by exporting from eclipse. If I export to a runnable jar file then I cannot control the making of the manifest file. I need to add the splash image in the manifest file. And I am having some other issues with the runnable jar export. Hence this path.
It seems that log4j is missing.it is expecting jars/log4j-1.2.16.jar in class path .
jars/log4j-1.2.16
should be under folder structure.
jars/log4j-1.2.16.jar
In which directory are you trying to use:
java -jar MyApp.jar
You can specify the packages/directories in the classpath to be sure they are found.
Ok, so according to this link,
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.
So, it seems I cannot include other jar files in my jar file.

Why has it failed to load main-class manifest attribute from a JAR file?

I have created a JAR file in this way jar cf jar-file input-files. Now, I'm trying to run it. Running it does not work (jre command is not found):
jre -cp app.jar MainClass
This does not work either:
java -jar main.jar
(Failed to load Main-Class manifest attribute from main.jar).
I also found out that
To run an application packaged as a
JAR file (version 1.2 -- requires
Main-Class manifest header)
What is the "Main-Class manifest header"? How do I create it and where do I put it?
I'm not sure I believe your symptoms:
If the jre command isn't found, then running jre -cp app.jar should give the same error
Just adding a JAR file to the classpath shouldn't give the error you're seeing
I'd expect you to see this error if you run:
java -jar app.jar
The Main-Class header needs to be in the manifest for the JAR file - this is metadata about things like other required libraries. See the Sun documentation for how to create an appropriate manifest. Basically you need to create a text file which includes a line like this:
Main-Class: MainClass
Then run
jar cfm app.jar manifest.txt *.class
set the classpath and compile
javac -classpath "C:\Program Files\Java\jdk1.6.0_updateVersion\tools.jar" yourApp.java
create manifest.txt
Main-Class: yourApp newline
create yourApp.jar
jar cvf0m yourApp.jar manifest.txt yourApp.class
run yourApp.jar
java -jar yourApp.jar
You can run with:
java -cp .;app.jar package.MainClass
It works for me if there is no manifest in the JAR file.
I got this error, and it was because I had the arguments in the wrong order:
CORRECT
java maui.main.Examples tagging -jar maui-1.0.jar
WRONG
java -jar maui-1.0.jar maui.main.Examples tagging
The easiest way to be sure that you have created the runnable JAR file correctly, with the appropriate manifest file, is to use Eclipse to build it for you. In your Eclipse project, you basically just select File/Export from the menu, and follow the prompts.
That way, you can be sure that your JAR file is correct and will know to look elsewhere if there is still an issue. The process is described in full in FAQ How do I create an executable JAR file for a stand-alone SWT program?.
I was getting the same error when i ran:
jar cvfm test.jar Test.class Manifest.txt
What resolved it was this:
jar cvfm test.jar Manifest.txt Test.class
My manifest has the entry point as given in oracle docs (make sure there is a new line character at the end of the file):
Main-Class: Test
Try
java -cp .:mail-1.4.1.jar JavaxMailHTML
no need to have manifest file.
I discovered that I was also having this error in NetBeans.
I hope the following is helpful.
Make sure that when you go to Project Configuration you set the main class you intend for running.
Do a Build or Clean Build
Place the jar file where you wish and try: java -jar "YourProject.jar" again at the command line.
This was the problem I was getting because I had other "test" programs I was using in NetBeans and I had to make sure the Main Class under the Run portion of the Project configuration was set correctly.
many blessings,
John P
I faced the same problem. This unix command is not able to find the main class. This is because the runtime and compile time JDK versions are different. Make the jar through eclipse after changing the java compiler version. The following link helped me.
http://crunchify.com/exception-in-thread-main-java-lang-unsupportedclassversionerror-comcrunchifymain-unsupported-major-minor-version-51-0/
Try running the jar created after this step and then execute it
If your class path is fully specified in manifest,
maybe you need the last version of java runtime environment.
My problem fixed when i reinstalled the jre 8.
If you using eclipse, try below:
1. Right click on the project -> select Export
2. Select Runnable Jar file in the select an export destination
3. Enter jar's name and Select "Package required ... " (second radio button) -> Finish
Hope this helps...!

Categories