I am unable to resolve this issue in which i was trying to executing a java program through bash script .
Why i am getting this error ??
Exception in thread "main" java.lang.NoClassDefFoundError: com/QuoteTester
Caused by: java.lang.ClassNotFoundException: com.QuoteTester
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.QuoteTester. Program will exit.
This is my script content quotetest.sh
java -cp ./com/ug_ugbapi-all.jar com.QuoteTester $1
I am executing the script , this way
./quotetest.sh GOOG
From the directory from where i am executing this script quotetest.sh , in that i got a directory by name com under which i got ug_ugbapi-all.jar and QuoteTester.class classes .
Tried every option (placing all the files in same folder com , but nothing worked )
Could anybody please tell me how to resolve it ??
With your given classpath, Java is only going to look in the jar file. For it to find the class file outside of the jar, you have to modify your -cp argument:
java -cp ./com/ug_ugbapi-all.jar:. com.QuoteTester $1
You are getting this error because the com/QuoteTester class cannot be found, which means that the com/QuoteTester class is not on the classpath.
Related
I am having trouble on setting up classpath on Mac. The following is the code I have attempted,
First I exported the jars using terminal
abc-MacBook-Pro:~ abc$ export
CLASSPATH=$CLASSPATH:/Users/abc/Desktop/IR/luceneJar/demo/lucene-demo-4.0.0.jar: export
CLASSPATH=$CLASSPATH:/Users/abc/Desktop/IR/luceneJar/core/lucene-core-4.0.0.jar: export
CLASSPATH=$CLASSPATH:/Users/abc/Desktop/IR/luceneJar/queryparser/lucene-queryparser-4.0.0.jar: export
CLASSPATH=$CLASSPATH:/Users/abc/Desktop/IR/luceneJar/analysis/common/lucene-analyzers-common-4.0.0.jar
Then when I echoed the classpath, following is the result
abc-MacBook-Pro:~ abc$ echo $CLASSPATH
/Users/abc/Desktop/IR/lucene-4.0.0 2/core/lucene-core 4.0.0.jar:
/Users/abc/Desktop/IR/lucene-4.0.0 2/demo/lucene-demo-4.0.0.jar:
/Users/abc/Desktop/IR/luceneJar/analysis/common/lucene-analyzers-common-4.0.0.jar
The error I am having is following when I tried to function the index file
abc-MacBook-Pro:~ abc$ java org.apache.lucene.demo.IndexFiles -docs
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/demo/IndexFiles
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.demo.IndexFiles
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Please let me know about the above error, thank you in advance. I am quite new with Lucene Java.
This is common Java classpath problem, nothing related to lucene (or mac) here.
Try this
1) Create lib folder and move all your lucene jars to lib
- lets say your project has two folders now lib and src, lib has all jars and src has yourJavaFile.java
2) Run below commands for compilation and execution from your src
- javac -cp ".:../lib/*" yourJavaFile.java
- java -cp ".:../lib/*" yourJavaFile
I have the following directory structure:
somedir/
lib/
myapp.jar
commons-io-2.1.jar
...lots of otherjars
From inside somedir, I try to run the following command:
java -cp lib/* net.myapp.Driver /home/myUser.blah.text
And I am getting the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: lib/commons-io-2/1/jar
Caused by: java.lang.ClassNotFoundException: lib.commons-io-2.1.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: lib/commons-io-2.1.jar. Program will exit.
Why is it telling me that it can't find a "main" in Apache Commons IO? I might be able to understand if it couldn't find my Driver class (maybe I misconfigured something), but this is just throwing me way off. Ideas? Thanks in advance!
From the stack trace it appears as if java is taking the commons-io Jar for a class which leads me to a wild guess: Could it be a whitespace problem? Is it possible that lib contains a jar with a space in it? Have you tried enclosing the cp option in quotes (i.e. use -cp "lib/*")?
Use quotes.
java -cp "lib/*" net.myapp.Driver /home/myUser.blah.text
Well explained here.
you could also try java -cp /lib/* net.myapp.Driver /home/myUser.blah.text
I seem to be unable to compile a simple MongoDB + Java Example:
I have this file
https://github.com/mongodb/mongo-java-driver/blob/master/examples/QuickTour.java
In my command line I compile by doing
$ javac -cp mongo-2.10.1.jar QuickTour.java
$ java -cp mongo-2.10.1.jar QuickTour
However it gives me the error
Exception in thread "main" java.lang.NoClassDefFoundError: QuickTour
Caused by: java.lang.ClassNotFoundException: QuickTour
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
What is going on and how can I fix this? Is it something to do with my compilation arguments?
Thanks...
It's not the compile time error. It's the error you got when you try to run your class. And the error simply says that it cannot find the class that you are trying to run. That error almost always means that you messed up with the classpath.
The problem is, you forgot to include the current directory in your argument to classpath. Add a dot(.) in addition to your mongo.jar file as the argument to -cp:
java -cp .;mongo-2.10.1.jar QuickTour
This assuming that you are executing your QuickTour class from the same directory where you have placed it.
The file FactoryDemo.java is compiled successfully and in it there is the
package home.city.Desktop.factorydemo;
I compile it by using javac -d / FactoryDemo.java and nothing wrong is reported.
But when I try to run it by using java home.city.Desktop.factorydemo.FactoryDemo, it reports:
Exception in thread "main" java.lang.NoClassDefFoundError: home/city/Desktop/factorydemo/FactoryDemo
Caused by: java.lang.ClassNotFoundException: home.city.Desktop.factorydemo.FactoryDemo
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)
Could not find the main class: home.city.Desktop.factorydemo.FactoryDemo. Program will exit.
I have tried many times but I could not find the solution to the problem. Thanks for your help in advance.
Your compiled class must be in a directory called:
home/city/Desktop/factorydemo
On Unix, just run this:
mkdir -p home/city/Desktop/factorydemo; mv FactoryDemo.class home/city/Desktop/factorydemo/
For best practice, simply move your Java source file in that folder too so the compiler outputs the file to the right spot.
FactoryDemo Class is created under root directory, you should type following command ( . instead of /)
javac -d . FactoryDemo.java
It creates home.city.Desktop.factorydemo directories starting from current directory.
If you want class created under root directory. You must type following command to execute.
java -classpath / home.city.Desktop.factorydemo.FactoryDemo
I'm trying to to run my freshly installed apache solr on my server (CENTOS 5.2 x86_64) and I get the followoing error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: start/jar
Caused by: java.lang.ClassNotFoundException: start.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: start.jar. Program will exit.
This is the commend I ran: java start.jar
As far as I'm aware everything is installed correctly.
Any ideas?
Thanks
C
Run it with java -jar, not java:
java -jar start.jar
The java command is used for running .class files, while the java -jar command actually executes the code in the jar properly, using the manifest file to interpret the jar's contents.