Getting ClassNotFound Exception while executing shell script - java

I want to write a shell script, which should call classes inside the jars. I am getting a ClassNotFound Exception when one jar's class file calling another jar's class file.
Here is what I've tried:
cd /home/appteam/encrypt/REPORT/CRYPTO4REPORT/
echo $1
echo $2
pwd
set JAVA_HOME = /App/jdk1.6.0_25/bin;
set path= /App/jdk1.6.0_25/bin;
java -classpath .:jars/log4j-1.2.8.jar:jars/xmlsec-1.4.2.jar:jars/CryptoUtils.jar:jars/bcprov-jdk15-146.jar:jars/commons-logging.jar:jars/Utils.jar:jars/Crypto.jar com.crypto.pki.PkiCrypto 1>> files/log/SuccessLog.log 2>>files/log/ErrorLog.log "ILI_RPT_TRV_07022013_001.enc" "crypto4decrypt.properties" DECRYPT
From the above code, I am calling com.crypto.pki.PkiCrypto class which is inside Crypto.jar. Form the PriCrypto class calling FileUtil class which is inside Utils.jar.
I receive a ClassNotFoundException when PriCrypto class file uses the FileUtil class.
I got following Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/common/utils/FileUtil
at com.crypto.pki.PkiCrypto.processFile(PkiCrypto.java:75)
at com.crypto.pki.PkiCrypto.main(PkiCrypto.java:31)
Caused by: java.lang.ClassNotFoundException: com.common.utils.FileUtil
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)
... 2 more

check Utils.jar is in the class path while running the program and also verify that utils.jar contains com.common.utils.FileUtil class

Related

"java.lang.NoClassDefFoundError" Running java using cmd but getting error

Running java using cmd following commands but getting error
java -cp %etc_folder%%service%%library%
i did set up environment variable for etc_folder, service, library
library envi: target\service_service-05.2015.jar
I check the folder jar file is in folder.
getting following error:
Exception in thread "main" java.lang.NoClassDefFoundError: target\service_service-05/2015/jar;
caused by: java.lang.ClassNotFoundException: target\service_service-05.2015.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)
I think it's replacing "." to "/"
not sure why, can anyone please help with this.
Thank in advanced!!
You should execute Java as following pattern:
java -cp $(echo /opt/path/lib/*.jar| tr ' ' ':') com.xxx.xxx.MainClass
All your jar putting into /opt/path/lib/,
com.xxx.xxx.MainClass is which class you have main() function.

Getting java.lang.ClassNotFoundException

I am able to compile code but while running it is throughing classnotfound
C:\Users\Desktop\New>javac -classpath java-mail-1.4.4.jar SendMail.java
C:\Users\Desktop\New>java -classpath java-mail-1.4.4.jar SendMail
Exception in thread "main" java.lang.NoClassDefFoundError: SendMail
Caused by: java.lang.ClassNotFoundException: SendMail
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: SendMail. Program will exit.
Can you suggest how to solve this?
Try using below commands
To compile java class
javac -classpath .;<jar file path>; Abc.java
To execute java class
java -classpath .;<jar file path>; Abc
Try using:
Windows
java -classpath java-mail-1.4.4.jar;. SendMail
note this ----^

java.lang.NoClassDefFoundError when using weka.jar file in my own java file

I'm having problems executing my java program. The code is placed in the file RunMining.java and contains multiple import weka.[..] lines. The weka library (weka.jar) is placed in the same folder.
I compile it:
$ javac -cp weka.jar RunMining.java
This creates three files: RunMining.class, RunMining$1.class, RunMining$classifierType.class
When i run it: java -cp weka.jar RunMining i get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: RunMining
Caused by: java.lang.ClassNotFoundException: RunMining
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)
My RunMining.java file only contains one class called RunMining, which contains the main method. I would prefer not to add the weka.jar file to my CLASSPATH since this file will be compiled using a makefile and executed using a script, on different machines.
Any suggestions, to how i can run the compiled files?
You need to add the classpath of your compiled files as follows:
java -cp weka.jar;. RunMining
This assumes your files are compiled in the root directory which your javac command suggests.

configuring classpath in java

I am new to java..
I have an example structure as follows:
/folder/foo.java /folder/bar.java /folder/foobar.java
Now I am trying to run foobar.java but I get the following exception
Exception in thread "main" java.lang.ClassNotFoundException: /folder/foobar
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.apache.hadoop.util.RunJar.main(RunJar.java:149)
Then I tried to configure it by doing
java -classpath . foobar
Exception in thread "main" java.lang.NoClassDefFoundError: foobar
Caused by: java.lang.ClassNotFoundException: foobar
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)
Any clues?
Thanks
The default value of a classpath is ‘.’ , i.e. the current directory. The value of the classpath environment variable overrides this value.
If the java command is issued with –cp or –classpath option, it overrides the default ‘.’ and classpath environment variable value.
Below is an example for setting a classpath during class execution
C:>java -classpath "." com.abc.example.SayHello
As opposed to compiling where you need to give exact path, to run the class file, we need to follow the package structure.
This is due to the way the Classloader tries to resolve the class location by combining its package and class name. You must be on the package root location and issue the java command specifying the package structure.
C:>java com.abc.example.SayHello
Hello!!
First, you need to compile java files:
javac /folder/*.java
Then you can run a class with main() function:
java -cp . folder.foobar

Could not find main class error when running a java wsdl client

I have written the following code, which is meant to be a client wsdl for http://www.nanonull.com/TimeService/TimeService.asmx?WSDL:
package time;
class Client {
public static void main(String args[]){
TimeService service = new TimeService();
TimeServiceSoap port= service.getTimeServiceSoap();
String result = port.getTimeZoneTime("UTC+10");
System.out.println("Time is "+result);
}
}
But when I try to run it with java I get the following:
C:\Program Files\Java\jdk1.6.0_22\bin>java client.Client
Exception in thread "main" java.lang.NoClassDefFoundError: client/Client
Caused by: java.lang.ClassNotFoundException: client.Client
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: client.Client. Program will exit.
Does this error mean I should be importing any classes?
The main class you wrote is time.Client, but you are trying to run client.Client. Better run it like this:
java time.Client
If this does not help, then you have a classpath problem - java cannot find the main class in the classpath. Set the classpath with -classpath option:
java -classpath classes-directory;list-of-jar-files time.Client
You need to set the classpath:
C:\Program Files\Java\jdk1.6.0_22\bin>java -cp . client.Client

Categories