Java File runs in eclipse but on command line - java

I have created a jar file in eclipse using maven and am able to run a class from the jar file with eclipse. However when I run the following command from the command prompt:
c:\recomendation_engine\recomendation\target>java -classpath recomendation-0.0.1
-SNAPSHOT.jar recomendation.ManageGeneralContent test
I get the following error:
Organization Environment test
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at recomendation.ManageGeneralContent.main(ManageGeneralContent.java:52)
I believe that the issue is that the class can not find the mysql driver. However it is in the classpath:
c:\Program Files\Java\jdk1.7.0_60\lib\*;C:\Users\User\.m2\repository\com\google\
guava\guava\18.0-rc1\guava-18.0-rc1.jar;C:\Users\User\.m2\repository\org\slf4j\s
lf4j-api\1.7.7\slf4j-api-1.7.7.jar;C:\Users\User\.m2\repository\com\jolbox\bonec
p\0.8.0.RELEASE\bonecp-0.8.0.RELEASE.jar;C:\Users\User\.m2\repository\postgresql
\postgresql\9.1-901-1.jdbc4\postgresql-9.1-901-1.jdbc4.jar;c:\recomendation_engi
ne\recomendation\lib\mysql-connector-java-5.1.6.jar;C:\recomendation_engine\reco
mendation\target\recomendation-0.0.1-SNAPSHOT.jar;
Can you please tell me what I am doing wrong?

You are overriding the CLASSPATH environment variable when you specify the "-classpath" switch on the java command line.
Try running:
c:\recomendation_engine\recomendation\target>java
recomendation.ManageGeneralContent test
Check out: http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html

Related

How to add jar files and compile using command prompt? (Error: A JNI error has occured,Please Check your Installation)

I forked one project from github and when i want to run it gives error. I added org.json jar file to jdk\lib and also set the Path variable to it. I compiled code but it got no errors but when i run it i get exception as follows : (and I am running it using command prompt)
Error: A JNI error has occurred, please check your installation and
try again Exception in thread "main" java.lang.NoClassDefFoundError:
org/json/JSONExceptio n
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException:
org.json.JSONException
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)
... 7 more
Add required jar to classpath like this:
For *nix-like systems:
export CLASSPATH=<path-to-app>/myapp.jar:<path-to-app>/lib/json.jar:.
java MyApp
For Windows:
set CLASSPATH=<path-to-app>/myapp.jar;<path-to-app>/lib/json.jar;.
java MyApp
...or via manifest:
Manifest-Version: 1.0
Main-Class: MyApp
Class-Path: /lib/json.jar

Trying to run a POI-dependent class on Command Prompt

As the title says, I'm trying to run a class in Command Prompt which uses the Apache POI library but when I run this command:
java -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs; HyperlinkWriter
I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/CreationHelper
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.CreationHelper
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)
... 6 more
the libs folder contains the entire POI folder that's zipped up when downloaded so I'm not sure why it's throwing this error. I also tested this with IntelliJ and it ran fine. I know that the way Command Prompt and IntelliJ handles libraries is different but I'm not sure why this is happening.
I would appreciate any help! Thanks!
You need to actually compile the jar with the original .java files and the specified POI jars.
javac -cp \path_to_jars \path_to_java_files
and then
java MyProject
I found the answer from a previous answer of StackOverflow. I had to add a '*' to the end, and a '.' after the semicolon. So with my example, in terms of compiling, I had to do:
javac -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs\*;. C:\Users\rperera\IdeaProjects\LinkingNames\src\HyperlinkWriter.java
and then to run it (Credit to #andrewdleach), I just did:
java HyperlinkWriter

Running class from JAR with multiple JAR dependencies via command prompt

I cannot understand why none of these are working. I've seen similar situations on stackoverflow and their solutions haven't worked for me.
I have a lib directory containing 15 JARs. When I run this command in command prompt from within that lib directory:
java -cp openjpa-2.2.1.jar org.apache.openjpa.enhance.PCEnhancer
I get a NoClassDefFoundError for one of it's dependencies.
Exception in thread "main" java.lang.NoClassDefFoundError: serp/bytecode/Instruction
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: serp.bytecode.Instruction
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)
... 7 more
Now when I try to add the jar containing that dependency:
java -cp openjpa-2.2.1.jar:serp-1.13.1.jar org.apache.openjpa.enhance.PCEnhancer
I get this:
Error: Could not find or load main class org.apache.openjpa.enhance.PCEnhancer
I've also tried:
java -cp openjpa-2.2.1.jar:serp-1.13.1.jar:. org.apache.openjpa.enhance.PCEnhancer
java -cp . org.apache.openjpa.enhance.PCEnhancer
With the same effect, and when I try:
java -cp serp-1.13.1.jar -jar openjpa-2.2.1.jar org.apache.openjpa.enhance.PCEnhancer [path to .java file]
All I get is:
>OpenJPA 2.2.1
>version id: openjpa-2.2.1-r422266:1396819
>Apache svn revision: 422266:1396819
>os.name: Windows 7
>os.version: 6.1
>os.arch: amd64
>java.version: 1.8.0_25
>java.vendor: Oracle Corporation
>java.class.path:
> openjpa-2.2.1.jar
With no file output, even when I add the -d flag with a directory.
Where am I going wrong?
RealSkeptic's comment was correct. Semicolons are the proper syntax to separate JARs listed on the classpath in Windows, like so:
java -cp jar1;jar2;jar3 classToRun args

I made a jar file, but it doesn't run in command line like it used to

I made an executable jar file with manifest and everything and when I ran it in the command prompt a while ago, it worked perfectly, but now, I do the same thing, I also recreated the jar a few times, it keeps giving me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: ComputingAverage (wro
ng name: me/kevinossia/mystuff/ComputingAverage)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
I am using the java -jar ComputingAverage.jar command. Why do I get this error?
Verify your MANIFEST file. It indicates to JVM how your program will run.
For more information, check this article: http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
You need to make sure the paths match in the jar file and the package header in the java file.
you just try my way. Create jar file using eclipse.
Right click on your project->export->java->runnable jar file-> next-> select ComputingAverage in launch configuration dropdown box->select destination export-> select Extarct required libraries into generated jar->finish.
open cmd :- give the command like this "java -jar jarFileName"
Thats it.
It should work.

Java programs cannot run after downloading opencsv

I downloaded the opencsv (from http://sourceforge.net/projects/javacsv/files/latest/download) and since then I have been getting this error with all my Java programs
java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
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)
Exception in thread "main"
How can I fix this? I realized I didn't need the opencsv anyway...how can I remove it so that my programs can compile and run as usual? I'm using Eclipse Version: 1.4.2.20120213-0813
You can remove a plugin by going to Help->About (or Eclipse->About on a Mac), choosing installations details, selecting the plugin you wish to remove, and choosing 'Uninstall'.

Categories