Executing Jar File in JDK 1.6 giving issues - java

I have created a simple project in java using eclipse ide to test the log4j functionality.
I need to run the application from the command prompt using the jar tool in java.
The logtest.java file is simple java file which defines a logger as below.
It is referencing a Logger which is in another Jar file. below is directory structure.
package com;
import org.apache.log4j.Logger;
public class logtest {
private static Logger logger = Logger.getLogger(logtest.class);
public static void main(String[] args) {
System.out.println("---------------HIIIIII-----------");
}
Below is the directory structure that i have created in the project in eclipse.
src/com/logtest.java
lib/log4j-1.2.17.jar
the manifest files entries are as below and i have manually created the maifest file.
Manifest-Version: 1.0
Main-Class: com.logtest
Class-Path: lib/log4j-1.2.17.jar
The contents of the Jar file are as below which i unzipped using the jar utility.
META-INF/MANIFEST.MF
.project
com/logtest.class
manifest.mf
lib/log4j-1.2.17.jar
.classpath
I have exported the jar file and when i try to run using the commond java -jar JarTest2.jar i am getting the following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.logtest.<clinit>(logtest.java:6)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
I know its a class not found exception but strange thing is that the same jar when i create using jdk1.5 and run command tool the jar runs successfully and the java class executes without any error.
I have no idea why this is going as everything is the same.
mine is a 64 bit windows 7 machine and the other machine where it is running is a 32 bit XP machine.
Of course the version of eclipse ide is different on both machines.
Can you please let me know is there something wrong about what i am doing or am i missing something else.
Thanks
Vikeng21

I'm pretty sure that Java doesn't support embedded Jars. I'd recommend moving the lib/log4j-1.2.17.jar file out to the file system (so that it creates a sub folder ./lib within the same drctory as your executable Jar & see if that makes a difference.
If you really want to use embedded Jars, you could take a read of One-Jar
You should have a directory structure of
.\JarTest2.jar
.\lib\log4j-1.2.17.jar
On your file system. The log4j Jar shouldn't be imbedded (contained within) the JarTest2.jar file

Please check your log4j.jar is duplicated. For example one log4j.jar was under lib folder and other was under Referenced libraries of eclipse. If it is duplicated, remove one jar and redeploy again.

Related

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook while running jar file

I have written a java program to convert input xml file into some other xml file. While doing this I am using excel file and for accessing xml file I used apache-poi libraries.
When I run my program in eclipse it works fine and gives required results. Now I export the project in jar file and I selected my main class while exporting into jar. But when I run the jar file, I am getting following exception:
D:\>java -jar EDR_to_DER_with_excel.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: com.unisys.xml.TestExcel. Program will exit.
The error is related to libraries that I am using. But I don't know why it's coming, because I am able to run the program in eclipse. Is the library class available in jar file> If not how can I give it to the jar while exporting?
It appears that the Apache POI Libraries are not in the classpath
Add the location of the libraries to PATH variable of your system (Assuming windows based on D:\ drive in question).
Export project do not included the external jars by default.
You have to add the third party lib to the classpath:
java -cp <lips> -jar EDR_to_DER_with_excel.jar
I got the solution. I followed simple approach.
-> Right click on project
-> Export
-> as Runnable jar
-> select radio button "Package required libraries into generated jar"
-> finish
This approach worked for me :)
I got the answer of the query in this case the jar files are missing in the program .Please check there are added from proper path.
This worked for me

java.lang.NoClassDefFoundError in Hadoop-1.0.1

i am working with Hadoop-1.0.1 in Eclipse. I am trying to run wordcount application but when i run my WordCount.java, Eclipse shows me following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.hadoop.conf.Configuration.<clinit>(Configuration.java:143)
at test.WordCount.main(WordCount.java:56)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 2 more
I have hadoop-core-1.0.1.jar in my classpath. Should the application require more jar
It seems you are getting this error because either two important logging classes commons-logging-1.1.1.jar and commons-logging-api-1.0.4.jar which are related with common logging is either not available from your classpath or you just don't have it.
In most cases these files are in \lib folder with other Hadoop Jar so when you set class path these jar are accessible. Please verify that you do have these files in your machine and are set in your classpath so they are accessible.
Also when you run "hadoop version" the version comes up as result, the same hadoop-core--SNAPSHOT.jar must be in your $HADOOP_HOME location.
You mentioned that you are running WordCount.java instead you should say that you are running WordCount job because you are always using the compiled application as jar not java.
$ bin/hadoop jar /usr/jboss/wordcount.jar org.myhadoop.WordCount /usr/jboss/wordcount/input /usr/jboss/wordcount/output
hadoop-core-1.0.1 is probably not in your classpath: commons-logging is actually included several times in the hadoop jar!
It seems that you did not include the common-logging jar file so Download Here the jar file depends on version, Then will included it in your program and run it THANK yoU

Java error using JARs, java.lang.NoClassDefFoundError: javax/mail/Store

I'm making a java application that uses Javamail, and it works great when I compile it. I want to make it into a jar file so it can be easily moved around and executed. The problem is I am getting this error when I try to run the jar from the cmd line
java -jar ActriveTray2.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Store
at stockApp.init(stockApp.java:11)
at ActiveTray.main(ActiveTray.java:31)
Caused by: java.lang.ClassNotFoundException: javax.mail.Store
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Here's what's in my jar file
images/tray.gif
META-INF/MANIFEST.MF
ActiveTray.class
ActiveTray.java
config.class
config.java
GmailFetch.class
GmailFetch.java
stockApp$1.class
stockApp.class
stockApp.java
I think the issue is classpath, where the jar doesn't know where the javamail jar lives. My classpath is set correctly when I execute the code (java ActiveTray2) it works fine... help :(
you need to add the jars you depend on to the manifest file, and also add them to the jar you are creating. java -jar will ignore your environment's classpath, by design.
From the java docs:
-jar: When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
so, you can add a line to your manifest file like:
Class-Path: mail.jar
to your manifest file.
When you launch a runnable jar file in java, the classpath is ignored, both the environment variable and the command line classpath. You have to specify it inside the manifest file of your runnable jar file.
Here is some information from wikipedia :
You manifest has to include :
Main-Class: org.mypackage.HelloWorld
Class-Path: lib/supportLib.jar
<additional line feed and carriage return>
The class-path will list the location of the librairies you use relative to the location of your main runnable jar file.
Regards,
Stéphane
The required mail implementation jars should be present in your classpath.
Same problem i faced in my project. The reason is jar file missing or not locate in class path. Two steps to solve this issue.
First add JavaMail API 1.4.7 jar to the class path.
Then add the same jar to server library. In my application i used Apache tomcat 7. i put the jar in lib folder

I can't load the JDBC driver for MySQL

I've been trying to load the JDBC MySQL connector with the following code:
import java.sql.*;
public class dbTest{
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
Class.forName("com.mysql.jdbc.Driver");
}
}
And I keep getting a class not found exception:
java.lang.ClassNotFoundException
at edu.rice.cs.plt.reflect.PathClassLoader.findClass(PathClassLoader.java:148)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at dbTest.main(dbTest.java:6)
I have added the path to the driver (mysql-connector-java-3.1.14-bin.jar) to my classpath and double checked to make sure it was correct. I also added copies of the jar to the ext folder of my Java installation based on what I read from this article: http://www.developer.com/java/data/jdbc-and-mysql-installation-and-preparation-of-mysql.html
I also searched through posts of others who have had this problem, but all of the responses so far have been saying to add the connector jar to the classpath, which I have already done.
Any help would be greatly appreciated.
I have added the path to the driver
(mysql-connector-java-3.1.14-bin.jar)
to my classpath
The exception tells you that you didn't do it correctly.
How are you setting CLASSPATH? If it's an environment variable, you're going to learn that IDEs and app servers ignore it. Don't use it.
Don't put it in the /ext directory of your Java JDK, either.
The right way to do it depends on how you're using it:
If you're running inside an IDE like Eclipse or IntelliJ, you have to add the JAR to a library.
IF you're running in a command shell, use the -p option for javac.exe when you compile and java.exe when you run.
If you're using it in a web app, you can start by putting it in the WEB-INF/lib directory of your WAR file. If you're using a servlet/JSP engine like Tomcat 6, put it in the Tomcat /lib directory.
On IntelliJ this is how I solved this problem:
File > Project Structure > Libraries > +
Locate the jdbc connector. For me it was on C:\Users\MyName.InteliJIdea13\config\jdbc-drivers
There are two classpaths in java. Build path and run path. Build path is used when compiling .java files into .class files. In a language like C you have a linker stage that fills in all the missing symbols when you run the linker on a bunch of object files. Thats why for .exe(windows) or other native binaries(linux) there is no run path. Java is slightly different because the compiled .class definitions get loaded by the jvm as they are needed.
What the net out of this is that you have to supply a runtime classpath to the jvm. At the command line you use java.exe which searches a few places by default including $CLASSPATH, the current directory/lib, and whatever you supply to the -cp option.
IDEs are different from the command line because they are attempting to shield you from some of the nastiness of running java.exe and supplying the locations where all the .class files are(which would be onerous on a large project).
Most IDE's have some sort of "Run Configuration" tab that allows you to specify certain libraries or locations with classes that will be used when you run your application. Below is how to set the run path in eclipse,netbeans, and intellij.
http://javahowto.blogspot.com/2006/06/set-classpath-in-eclipse-and-netbeans.html
http://www.jetbrains.com/idea/webhelp/run-debug-configuration-application.html

How to create an exe file in java

Hi I want to create an exe file for my java app.
I tried with some third party softwares JEXECreator, successfully created the exe file and its working fine in my system, when I tried with another machine, it’s not working. I got the following error
* The error occurred while running the application. The exit code is 0x10000223.
* Contact the vendor of the application for troubleshooting.
java.lang.ClassNotFoundException: com.sample.SampleMain
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.ucware.JEXEClassLoader.run(Unknown Source)
at com.ucware.JEXEClassLoader.main(Unknown Source)
**************************************
I know there is something wrong with the classpath which I set.
Actually I want to create the exe file myself without using any third party software.
I found the steps in lot of sites
Created the manifest file named Sample.mft with following contents
Manifest-Version: 1.0
Main-Class:
Class-path:
In this I have some doubts,
How the Main-Class should be added, with the full package name (com.sample.SampleMain) or the class name alone (SampleMain) or with the extension (SampleMain.class)
How the class-path should be added, I have 4 java classes and 2 jars in my project. How to add all these in the class path, and do I need to add the java jdk in classpath.
Where the manifest file should be saved
What should be the manifest file extension (mf or mft)
In command prompt from which directory I should create the exe file (from my project folder or src folder or the folder which contains all the java classes)
What’s the syntax should be used while creating jar in command prompt
(jar cmf Sample.mf Sample.jar Sample1.class Sample2.class Sample3.class Sample4.class jar1.jar jar2.jar) like this or (jar cvfm Sample.jar sample.mf *.class)
When I did something like this I am getting a jar instead of exe file, When I run the jar in command prompt using "java -jar sample.jar" then I am getting class not found exception".
Actually how to create an exe file instead of jar file, that means just by double clicking that exe file, should run my app in any machine.
Can anyone help me to do this?
Thanks in advance.
I use the Ant tool under Eclipse IDE to work with InnoSetup and Launch4J to create the EXE and its installer which it also manages the classpath...
A guide? You can refer to:
http://www.eteks.com/tips/tipCreationExe.html (in French)
Can't speak for JEXECreator, but I can recommend JSmooth (http://jsmooth.sourceforge.net/). I've successfully used it for several projects (e.g. this SWT based Java app).
Personally, I like JSmooth. That is just wrapping. This means it is still a Java application. When executing the exe, it will unpack the jar to a temporary folder and then execute it with javaw -jar ...
A second option is gcj. But that is absolutely a bad choice. That doesn't wrap the jar in an exe, but it really compiles it to native system code. But this slows down your application very much. You can check some results of my timing on this topic.
I had some positive experience with Excelsior JET. Unlike gcj it actually works and execution times are faster than that of an executed .jar file. The downside is that it's not for free.
Well I will recommend you to create one bash file instead of doing complex things and by double clicking on it you can run your application.Yeah but you cant change its icon but there are many free tools are available by using which you can easily convert bash file to exe.
To create an exe file, I use launch4j. Launch4j converts the jar file into exe file and if I wanna pack it for the installer, I use InnoSetup. For me, that exe created by launch4j works on pcs.

Categories