Program runs fine in IDE but not as Jar file - java

I have exported a jar file that I want to run the console. The code compiles and runs correctly in eclipse but I am having an issue running it from the console.
To me it looks like the referenced jar's I added via built path in the Eclipse project file and not being added to the export. If that is the case, how do I ensure that they do? If not, what am I doing wrong?

When you export your source code's class files to a jar using eclipse, only the .class files of your source are exported! Hence your exported jar file doesn't contain the referenced jars you mentioned in eclipse! Due to this, the error occurs while executing from command prompt.
Solution:
Take all the jar files required to execute the program, store it in the same directory as you store the exported jar file. Now while executing the java command, provide all the jar file's names in classpath field as following:
java -classpath .;JAR1.jar;JAR2.jar MainClass
Once you do this, your problem should be resolved!

The dependencies need to be on the classpath, i.e. run like this:
java -cp <path_to_jar1>;<path_to_jar2> -jar ScrumTimeCaptureMaintenence.jar

When running from the command line make sure any dependencies are set on the class path by listing them in the -classpath parameter

Related

Javac does not create Main Class Reference

I am trying to package up some java code which has references to the Hadoop API for java. I am writing the code on my PC and moving it to a CentOS VM and then compiling it.
For some reason when I run the javac command and then un-jar the product file my ./META-INF/MANIFEST.MF file does not have a line which defines the main class such as "Main-Class: folder1.folder2.file". I believe this is why I am getting a "no main manifest attribute when trying to run Hadoop.jar".
Just to step through the process I have the code written using netbeans and import the Hadoop API .jar file. After saving I upload Hadoop.java (this is the only file with code other than the Hadoop API) file to the VM in the /usr/dan directory.
From /usr/dan I run javac -classpath /usr/hadoop-0.20.2/hadoop-0.20.2-core.jar -d ./Hadoop ./Hadoop.java there aren't any errors displayed.
I then use jar cvf ./Hadoop.jar -c ./Hadoop/ to create the jar file. There aren't any errors displayed.
I then try to run the jar file with java -jar Hadoop.jar and get the error.
Is the only important file I need to upload to the VM the .java file? I have the Hadoop API jar file in /usr/hadoop-0.20.2/hadoop-0.20.2-core.jar?
Once I execute the javac command the API is included in the .class file and I no longer need /usr/hadoop-0.20.2/hadoop-0.20.2-core.jar, correct? Or does this file need to be retained in a certain path relative to the .jar file?
What are the possible reasons the manifest file would not have a Main method defined in it?
Much appreciation for any help, I've been racking my brain on this all weekend.
EDIT:
By default the manifest include nearly empty, as stated here:http://docs.oracle.com/javase/tutorial/deployment/jar/defman.html. I can easily add an entry to the manifest file with the m flag. I based my usage off of http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html. It seemed to me that this was something which had to be done after the jar file was created, but just adding the m flag with the appropriate components when initially jarring the file will do the trick in one shot.
If you create your jar through the jar command and want additional manifest entries (such as Main-Class), you'd need to use the m option
jar cmf manifest-file jar-file content-files
with a suitable manifest file (which can contain plain manifest entry lines).
In hadoop, it is not necessary that you need to have a Runnable Jar to execute. Hadoop come up with a utility for running Jars. You may use the following command for executing a Jar, which is not Runnable (Main class is missing in ./META-INF/MANIFEST.MF)
hadoop jar Hadoop.jar <PackageName>.<MainClassName> /input /output
If you don't have any packages use the following command
hadoop jar Hadoop.jar <MainClassName> /input /output

Jar file won't execute, not sure if I'm creating it correctly

I'm trying to make a jar file out of two classes, one of which depends on an external jar. I have a directory with a manifest.txt, a lib folder containing the external jar RXTXcomm.jar, and a folder named Arduino containing my two classes, SendValue.java and SerialClass.java.
First I'm compiling my classes using:
javac arduino\*.java
This creates 3 new files, SerialClass$1.class, SerialClass.class and SendValue.class. To make the jar file, I'm running:
jar -cfm send.jar manifest.txt arduino\*.class lib\rxtxcomm.jar
This works fine. I then try to run the file using:
java send.jar
I get the error:
Could not find or load main class send.jar
I've also tried to run it with the following command, and got the same error:
java -cp . send.jar
The only line in my manifest.txt is :
Main-Class: Arduino.SendValue
My classes run fine in Eclipse, so I'm assuming they're not the problem. SendValue.java has the line:
public static void main(String[] ag) {
as it's supposed to.
Any ideas?
You want to run your jar using:
java -jar send.jar
Also, unless you want to do some magic with nested jars with some tool like OneJar, you should remove lib\rxtxcomm.jar from your jar command, and add the following line to your manifest.
Class-Path: lib\rxtxcomm.jar .
If you're going to use the command line or terminal for running a single java executable, then use
java -jar send.jar
Also it would be better if you went through compiling and testing using an IDE such as Netbeans, IntelliJ IDEA, or Eclipse as they're meant to do such stuff and are more reliable on doing the steps unlike the human brain.

Making excutable jar file using eclipse

My project runs fine from Eclipse.
But when I tried to make it into a jar file or executable file it doesn't work.
I used the option "Export-Runnable JAR file"
The following message appears just after the eclipse finished the exporting process
JAR export finished with wornings , see details.
the details were ..
Exported with compile warnings:Mario/src/Map.java
and the same for other classes like
Exported with compile warnings:Mario/src/Player.java
and so on.
So that I used the other option "Export - JAR file"
It works fine and nothing appears while exporting it from Eclipse.
But when I try to open the file it gives me
Couldn't find the main class:Frame.Program will exit
Somebody have any idea about what the problem is?
Your MANIFEST.MF file inside the META-INF dir should have a Main-Class attribute pointing to your main class.
The important thing for executable jar is Manifest. Make sure it exists and points to the correct class with main method

Java Classpath problem

I have a Java Program that references a Jar File.
The Jar File is present in the same directory as the .class File but when I try to run the program from console, I get NoClassDefFound error :-(
Alternatively it runs Ok from Eclipse.
Why ?
The java program will not automatically include JAR files in its class path. You add a JAR file to its class path via the -classpath option. For example:
java -classpath .;yourJarFile.jar your.MainClass
Eclipse is automatically adding the JAR file whenever you run the program.
Note: The -classpath option expects a semicolon (;) when running on Windows and a colon (:) otherwise.
JAR files are not automatically included in the classpath. You can add a Class-Path entry and a Main-Class entry to the to JAR file containing the main method that you wish to execute. Then you can execute your code like so:
java -jar yourJarFile.jar
See the JAR File Specification for more details.
or specify the classpath on the command line:
java -classpath lib1.jar:lib2.jar:yourJarFile.jar your.MainClass

The dreaded java.lang.NoClassDefFoundError

I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth

Categories

Resources