How to solve java.lang.NoClassDefFoundError when creating bat file? - java

I am trying to run bat file which should start my restCRUD server written in java. Project contains only one main method so my bath file looks like this:
java -jar TaxAppWebserver.jar
pause
The problem is that when I run bat file I get java.lang.NoClassDefFoundError.
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Persistence
at facade.FacadeLogic.<init>(FacadeLogic.java:17)
at restCRUD.HandlerLogger.<init>(HandlerLogger.java:14)
at restCRUD.RestFileServer.run(RestFileServer.java:22)
at restCRUD.RestFileServer.main(RestFileServer.java:36)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
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)
Line 17 from the FacadeLogic.java:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TaxAppWebserverPU");
Can anyone give me a clue of what is wrong?

Rebuild your jar file with the missing jar added to the classpath in the MANIFEST.MF file. IDE's such as Eclipse can add these automatically when creating executable jar files
Class-Path: javax.persistence.jar

I solved the problem by creating lib folder in the project directory and copying all the external libraries to this folder. Also, I had to change my bat file for
java -cp TaxAppWebserver.jar restCRUD.RestFileServer
so that it knows where the main method is.

Related

JAR can't find the class of referenced library outside the working directory

I referenced one external library (Opencsv) in my project.
I added the Manifest file into the 'src' folder as:
Manifest-Version: 1.0
Main-Class: DataClean.DataPreprocess
Class-path: jars/opencsv.jar
The 'jars' is a folder contains the opencsv jar file in 'src' folder.
The complied main class is in 'DataClean' folder in 'src'.
When I create the jar, I use:
jar cfm PromoPrice_CN.jar Manifest.txt *
This command was run within 'src' folder.
Then, the JAR I created can only run within this 'src' directory. Otherwise, it gives the exceptions:
Exception in thread "main" java.lang.NoClassDefFoundError: com/opencsv/CSVReader
at DataClean.DataPreprocess.dataClean(DataPreprocess.java:154)
at DataClean.DataPreprocess.main(DataPreprocess.java:229)
Caused by: java.lang.ClassNotFoundException: com.opencsv.CSVReader
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)
... 2 more
The command I used to run the jar is:
java -jar C:\...\src\PromoPrice_CN.jar X Y Z
X, Y and Z are three input parameter for the jar.
Please help where did I do wrong?
FYI, the creation of the JAR has to be in command line for other reasons.
Thx

Packing Ressources into Jar and accessing them

I'm fairly new to Java and I'm having trouble exporting my application into a runnable jar.
My application runs some transformations using Saxon and I want the stylesheets used to be inside the runnable jar.
Right now I simply have a subfolder in my project in eclipse and I'm accessing the stylesheets with that path.
However as soon as I export the project as runnable jar inside that jar all the stylesheets are on the same level as the used libraries (subfolder is nowhere to be found.)
Also the application searches for that subfolder in the cwd and of course can't find it there.
How do I pack the stylesheets and access them properly inside the jar ?
//Update:
I think I'm a little further.
I've created a subfolder under src and put all stylesheets in there. I'm accessing them like so:
xsltexps = comp.compile(new StreamSource(new File(this.getClass().getResource("/stylesheets/" + stylesheets).toURI())));
If i run it in eclipse everything works fine. When I export it as runnable jar and run it i get the following exception:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.(Unknown Source)
I have no idea what that even means.
Okay, so here's what actually worked:
xsltexps = comp.compile(new StreamSource(
getClass().getResourceAsStream("/stylesheets/" + stylesheets)));
Most importang change is the use of getResourceAsStream instead of getResource

"Could not find the main class" in Java Salesforce application

I've been building an application in Eclipse which extracts data from salesforce and using an imported CSV file, updates and inserts records. It works fine from within Eclipse and I'm happy it's ready for deployment, but I cannot get the application working if I try and run it from cmd or double click the Jar. I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: uploader/jar
Caused by: java.lang.ClassNotFoundException: uploader.jar
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: uploader.jar. Program will exit.
I've been all over google and tried every solution out there, but can't find why it won't work. Here's the steps I'm using to build the jar and run it, please if anyone can show me where I'm going wrong, I'd be delighted! I'm not a java programmer...
The project is called uploader as is the package, by the way
Right click on project and click Export. Choose "Jar file" and
Next.
Select my project as the resource and ensure .classpath and .project
files are selected. Choose to Export generated class files and
resources, to Compress the contents of the JAR file, and choose the
location for the Jar file to go, then hit Next.
Choose to Export class files with compile errors, and warnings.
Choose to Save the description of this JAR in the workspace, and
give the location as /uploader/jardesc.jardesc. Hit Next.
Choose to Generate the manifest file, Save the manifest in the
workspace and Use the saved manifest in the generated JAR
description file.
Point the Main Class to the correct place by clicking Browse and
choosing the only option that I'm given, which is uploader.Main,
then click Finish.
If I now open the manifest file, I see the following:
Manifest-Version: 1.0
Main-Class: uploader.Main
So that looks correct to me.
However, if I open a command prompt, cd to the directory containing the jar file then say java uploader.jar, that's when I get the error above.
Can anyone see anything that looks wrong? I can't get past this point!
If I run with java -jar uploader.jar, I get this error instead:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sforce/ws/ConnectionException
Caused by: java.lang.ClassNotFoundException: com.sforce.ws.ConnectionException
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: uploader.Main. Program will exit.
Perhaps that can help pinpoint the problem?
*EDIT*
I've edited the manifest.mf file to this, but I'm still getting the error:
Manifest-Version: 1.0
Main-Class: uploader.Main
Class-Path: lib/enterprise.jar lib/wsc-22.jar
The problem seems to be a missing classpath setting, i.e. a definition of the classes and libraries that are needed in addtion. In your case it seems like at least some salesforce jar has to be added to the classpath.
Either put the classpath into the manifest file or add it at the command line: java -cp <classpath here> -jar uploader.jar

Problem when creating Jar file using Command Line

I have tried to create Jar file using Command Line.
My Manifest file:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class:Home
My Files and Location of that files:
Location : D:\Application
Files :
images
add.png
home.png
minus.png
Database.java
Home.java
UiDesign.java
Database.class
Home.class// This is my main class
UiDesign.class
Manifest.txt
mysql-connector-java-5.1.15-bin.jar
To create jar file i tried:
D:\Application>jar cmf Manifest.txt MyApp.jar *.class mysql-connector-java-5.1.15-bin.jar images
But the jar file is created. If i click that jar file, Error message shows like below,
Failed to Load Main-Class manifest attribute from
D:\Application\MyApp.jar
Thank for all....My problem solved by adding new line in Manifest File....Thank you all...
But now i have another problem.....
D:\JavaApplication-13-8-2011\Application>jar cfm MyApp.jar Manifest.txt *.class
mysql-connector-java-5.1.15-bin.jar images
D:\JavaApplication-13-8-2011\Application>java -jar MyApp.jar
Connect to MySQl
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Database.getDBConnection(Database.java:14)
at UiDesign.<init>(UiDesign.java:58)
at Home.main(Home.java:6)
java.lang.NullPointerException
at Database.getBrand(Database.java:31)
at UiDesign.<init>(UiDesign.java:59)
at Home.main(Home.java:6)
Exception in thread "main" java.lang.NullPointerException
at UiDesign.<init>(UiDesign.java:64)
at Home.main(Home.java:6)
D:\JavaApplication-13-8-2011\Application>
I think this problem occurs due to to class path not set to mysql-connector....I have added this jar file in my Application.....How to setclass path and run my Application jar file successfully.....
please help me....
Thank you...I cleared..this problem also...As Trisstan said, I added classpath in Manifest file...Now My Application Jar file Run Succesfully.....
Thank You all for your Quick Response......
Add a newline at the end of your manifest file.
Warning: 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.
From: http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
It may be an error about classpath, because your jar depends on mysql-connector-java-5.1.15-bin.jar
make sure there is a carriage return in your manifest file and I am also guessing your jar file name should precede the manifest file as your option says cfm.
Failed to Load Main-Class manifest attribute from D:\Application\MyApp.jar
From the error it looks like it thinks that the jar file is your manifest file. That should be the problem.
update the argument or change the option parameter to mcf I guess..
For your second problem:
Your jar probably contains a copy of the mysql jar. That does not work because jars are supposed to contain classes. The simplest way to fix this is to indicate in your manifest that your code depends on the mysql jar. Basically add the following:
Class-Path: mysql-connector-java-5.1.15-bin.jar
In your jar creation command, do not include the mysql jar. Finally, make sure that your jar and the mysql one are distributed together (in the same directory).
If you really want a single jar, extract the mysql jar and include its contents in the jar you are going to distribute.

Why doesn't a runnable JAR exported from Eclipse work?

I have a project which runs fine from within Eclipse.
But when I export it to a runnable JAR with dependencies packed into it it won't run.
The error, when running the JAR from console is:
EDIT (didnt run it with -jar before):
INFO: Loading XML bean definitions from class path resource [applicationContext-
framework.xml]
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOExc
eption parsing XML document from class path resource [applicationContext-framewo
rk.xml]; nested exception is java.io.FileNotFoundException: class path resource
[applicationContext-framework.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:349)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:113)
at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:80)
at org.springframework.context.support.AbstractRefreshableApplicationCon
text.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtain
FreshBeanFactory(AbstractApplicationContext.java:422)
at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:93)
at com.horstmann.violet.UMLEditorApplication.getApplicationContext(UMLEd
itorApplication.java:111)
at com.horstmann.violet.UMLEditorApplication.<init>(UMLEditorApplication
.java:94)
at com.horstmann.violet.UMLEditorApplication.main(UMLEditorApplication.j
ava:84)
... 5 more
Caused by: java.io.FileNotFoundException: class path resource [applicationContex
t-framework.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPat
hResource.java:143)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:336)
... 20 more
but the file is in resources/ from the JAR-root.
What am I missing or doing wrong?
Try to click on the option “Package required libraries into generated JAR”.
A JAR file is a regular compressed archive. Make sure that all the required libraries in your MANIFEST.MF file are present within the generated file.
Try to run the JAR from the command line - it will most likely display a useful error message.
There probably are some dependencies that eclipse doesn't know about or which aren't configured correctly within it, and are therefore missing from the JAR.
In general, it's not a good idea to rely on IDE functionality to produce deliverables - it depends on too many hidden factors and can't be automated for a build server. Any project that's beyond the experimental/toy stage should have an automated build via Ant or Maven.
The message
Could not find the main class: violet-0.21.2-SVN.jar. Program will exit.
indicates that you have gotten the Main-Class: MyPackage.MyClass line wrong in the Manifest when packaging together the jar file. You need to give the full name of the class with the main(String[] args) method.
See http://download.oracle.com/javase/tutorial/deployment/jar/appman.html for details.

Categories

Resources