Exception when running java code through command line - java

I have a simple Class
package chapter10;
public class CompilationTest {
public static void main(String[] args) {
System.out.println("HELLO WORLD");
}
}
The path is
Test\src\chapter10\CompilationTest.java
I successfully compiled the code into the same folder and now I have
Test\src\chapter10\CompilationTest.class
However when I try to run from the same folder it I get this error
>java CompilationTest
Exception in thread "main" java.lang.NoClassDefFoundError: CompilationTest (wrong name: chapter10/CompilationTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
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$000(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)
Could not find the main class: CompilationTest. Program will exit.
When I run using
>java chapter10/PropertiesTest
Exception in thread "main" java.lang.NoClassDefFoundError: chapter10/PropertiesTest
Caused by: java.lang.ClassNotFoundException: chapter10.PropertiesTest
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: chapter10/PropertiesTest. Program will exit.

The class is in the chapter10 package. Run it from the parent directory as:
java chapter10.CompilationTest
The reason for going to the parent is that Java is searching its CLASSPATH, which includes the current directory, for a chapter10 directory containing a CompilationTest.class file. You could also add the src directory's absolute path to the CLASSPATH to achieve the same effect:
set CLASSPATH=C:\...\Test\src
java chapter10.CompilationTest
Or (apparently this is better style) add the -cp or -classpath argument to java:
java -cp "C:\...\Test\src" chapter10.CompilationTest

Run using java chapter10.PropertiesTest. The Java command expects the fully qualified Java class name, not the pathname of the bytecode file.
The Java command locates classes to be loaded by looking on the classpath. This is a list of directories or JAR files to be searched in order. The algorithm used is roughly as follows:
Take the fully qualified name of the class, replace "." characters with the applicable file separator character (e.g. "/" or "\"), and append ".class" to the end. The result is a pathname.
For each entry in the classpath, lookup the pathname relative to the entry.
If the lookup succeeds, load the corresponding file.
Otherwise, continue to the next classpath entry.
If no lookup succeeded, throw a ClassNotFoundException.
The classpath can be set using an explicit command line parameter (-cp <path>) or it can be picked up from a CLASSPATH environment variable. If no classpath is set by either of these methods, a default classpath of "." is assumed, where "." denotes the current directory.
As #BalusC says, best practice is to use the "-cp" argument or create a wrapper script to launch your application. Relying on your shell's $CLASSPATH is a bit flakey, because you never know when it might be overridden or reset.
(This is a simplified description. It does not cover "bootclasspath", classpaths set in loadable JAR files, classloading from URLs, how inner/anonymous classes are handled, etc, etc.)

Related

Running java file in cmd - error given

I recently moved a project from one machine to another but I am now given an error when attempting to run java files on the new machine. I'm trying to run a java file from part of an Android project in command prompt but I am given an error. The file compiles okay but fails to run. Here is the error I am given;
Exception in thread "main" java.lang.NoClassDefFoundError: ChatServer (
e: edu/UTEP/android/ChatServer)
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)
Any ideas why I am receiving this error?
Your ChatServer is having a package declaration of edu.UTEP.android in the top. So either you need to remove that package declaration recompile your java file again and run it
(or)
you need to create folder structure like this
CurrentDir/edu/UTEP/android/
Keep your java file in android folder and invoke java ChatServer from CurrentDir. Any of these two will solve the issue :-)
For more info on packages, you can refer to my previous answer on a similar issue here
Make sure that the .jar that provides ChatServer is in your runtime Classpath.
This issue rise when jvm don't file class path.
check where u have placed your jar file or lib folder.
Move to that directory then run javac and then java .
Issue will be resolved.

NoClassDefFoundError for aurora.jar

I am trying to execute next:
C:\dev\sources\boSchedules\loadJavaLibs>java -cp aurora.jar; ojdbc6.jar
oracle.aurora.server.tools.loadjava.LoadJavaMain -thin -user login/pass#myserv:mysid
%BOS_SRC%/credit/card/api/ScheduleCardApi
And I get next:
Exception in thread "main" java.lang.NoClassDefFoundError: ojdbc6/jar
Caused by: java.lang.ClassNotFoundException: ojdbc6.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: ojdbc6.jar. Program will exit.
Why am I getting this error?
Take a look at the Java Application Launcher man page.
java -cp aurora.jar; ojdbc6.jar
oracle.aurora.server.tools.loadjava.LoadJavaMain -thin -user sched/sched#teach:prod
%BOS_SRC%/credit/card/api/ScheduleCardApi
You have a space between your classpath entries aurora.jar; ojdbc6.jar. The launcher thinks the first jar is the only classpath entry and the ojdbc6.jar is your class containing the main(String[] args) method. It also considers everything after that as arguments to pass to the main(String[] args) method. Remove that space.
Remove the space between the aurora.jar; and the ojdbc6.jar
Spaces delimit the parameters each other. The JVM interprets the command as if you run "ojdbc6.jar" class: "jar" as a classname and "ojdbc6" as a package.
To concat the names of libraries you want to place into classpath for a specific class run please use semicolon with no spaces as "lib1;lib2"
P.S.: Could you please ask your colleagues if you may paste some of our credentials to the SO? :)

TestNG command line not working (java org.testng.TestNG )

i'm trying to run testng tests using command line like this :
C:\Documents and Settings\Administrateur\Bureau\automatic tests testNG>java -cp
C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\
testng-6.3.1.jar org.testng.TestNG testng.xml
but i'm getting this error :
Exception in thread "main" java.lang.NoClassDefFoundError: and
Caused by: java.lang.ClassNotFoundException: and
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: and. Program will exit.
So How to add testNG to the classpath.
Thanks
The spaces in the filename are confusing things - Java thinks that only the c:\Documents part is in the classpath argument. Try this:
java -cp
"C:\Documents and Settings"\Administrateur\.m2\repository\org\
testng\testng\6.3.1\testng-6.3.1.jar org.testng.TestNG testng.xml
(where the whole thing is on one line and there's no break after "org" of course)
If you want to run via java on command line, you'll need to either enquote your path ("C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\") or alter slightly as follows: C:\Documents%20and%20Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\. You may also want to run via maven to avoid having to specify this classpath info.

unable to run java class from command prompt refering to a jar in classpath

I am trying execute a java class which accesses a method in a jar file.My package structure is com.xmlpost.XmlPostInit.java and it resides under the directory:
D:\Workspace\Test\bin
I am using the command below. Note, cs.jar contains the class com.xmlpost.XmlPostInit
D:\Workspace\Test\bin>java -classpath D:\FatWire\JSK\7.6.0\App_Server\apache-tomcat-6.0.18\webapps\cs\WEB-INF\lib\cs.jar com.xmlpost.XmlPostInit
.. which gives me following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/xmlpost/XmlPostInit
Caused by: java.lang.ClassNotFoundException: com.xmlpost.XmlPostInit
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.xmlpost.XmlPostInit. Program will exit.
You have to add your bin directory to the classpath too, as the JVM needs to look inside it to find your class. As you are running inside it, you can refer localy it with ".":
D:\Workspace\Test\bin>java -classpath .;D:\FatWire\JSK\7.6.0\App_Server\apache-tomcat-6.0.18\webapps\cs\WEB-INF\lib\cs.jar com.xmlpost.XmlPostInit

java - missing main class

I'm trying to run one of the very first examples from the book "Head First Java";
public class MyFirstApp {
public static void main (String[] args){
System.out.println("I Rule!");
System.out.println("The Worlds!");
}
}
"javac" created a .class file from the .java file - but "java" complains about a "missing main class" when trying to run the .class file (i also tried java -cp . "..." with the same result):
C:\>java \hfj\MyFirstApp.class
Exception in thread "main" java.lang.NoClassDefFoundError: \hfj\MyFirstApp/class
Caused by: java.lang.ClassNotFoundException: \hfj\MyFirstApp.class
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: \hfj\MyFirstApp.class. Program will exit.
You need to run it as
javac MyFirstApp.java
java MyFirstApp
From the directory where MyFistApp.java lives.
'javac' calls the compiler - to that you need to pass your .java file.
'java' will run the compiled code - to that you pass the name of your compiled file, but without any extension: "java MyFirstApp"
Specifying the full path of the file should work when you are not in that directory. But are you in the directory that holds the javac and java programs? If not, those may also need absolute paths if you have not put them on your PATH variable.
What ts the package name? Maybe you have something like
package org.test;
in your header?

Categories

Resources