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?
Related
I'm new to JAVA and trying unable to figure out what's wrong with the project I created.
I created a JAVA project in eclipse and exported a jar (not runnable jar).
I unchecked .project and .classpath file options while exporting. Now I'm trying to run that jar and getting NoClassDefFoundError:
I'm pasting the command and the errors below:
C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar -jar D:\Web\HbaseGateway\bin\HBaseGateway.jar
Exception in thread "main" java.lang.NoClassDefFoundError: py4j/GatewayServer
at hbase.gateway.HBaseGatewayEntryPoint.main(HBaseGatewayEntryPoint.java
:22)
Caused by: java.lang.ClassNotFoundException: py4j.GatewayServer
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)
... 1 more
Now, interesting thing is I'm able to run the main class using the following command.
In the below command I removed the -jar switch and passed the main class to java.exe.
C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar hbase.gateway.HBaseGatewayEntryPoint
Gateway Server Started
Why NoClassDefFoundError is being thrown? Am I missing something here?
For -jar option to work, you must add Main-class key in manifest.
Refer the java command documentation http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html
-jar option
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name.
In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point.
When you use the -jar option, the classpath is read from the MANIFEST.mf file as well. From the docs for the -jar option:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
This means that you have to add all your dependencies in a Class-Path section of the manifest file.
I have downloaded a java developers kit for my 64bit windows 7, wrote down my code in the notepad, though the code is compiling from the command prompt and creating a .class file, but its refusing to run showing the error code:
java.lang.NoClassDefFoundError: first Caused by: java.lang.ClassNotFoundException: first
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: first. Program will exit. Exception in thread "main"
I have made sure more than once that the file name and the class name are exactly same(i have kept them smallcase 'a' just to be sure). But still no avail, could you please suggest a few solutions please.. I'm new to java i'm basically a C/C++ programmer.
A java program has this basic structure:
ClassName.java
public class ClassName
{
public static void main(String[] args)
{
}
}
try using this outline to generate your code.
compile and run with:
javac ClassName.java
java ClassName
I used to get this error when I ran a Class file.
Try doing: java NameOfClass
You don't need the .java extension when running, but you need it for compiling. That was always the problem I used to have.
Did you set your classpath?
http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
java -classpath <path> <classname>
You must provide with the code.
Anyway, from the Java Docs, class ClassNotFoundException:
Thrown when an application tries to
load in a class through its string
name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader.
The loadClass method in class ClassLoader.
but no definition for the class with the specified name
could be found.
Must read link : Tip: Causes of java.lang.ClassNotFoundException
It is quite possible after compiling your code you would be writing java filename.java.
Because this sought of exception occurs then.
After compiling your program using javac filename.java use the command to start your interpreter java filename.
As you enter this command java interpreter automatically starts interpreting filename.class.
commands :
javac filename.java // to start compiling
java filename // to start interpreting
class HelloWorld
{
public static void main(String[] args)
{
System.out.println("hey");
}
}
Command prompt session:
C:\Users\zobdos\Desktop>javac HelloWorld.java
C:\Users\zobdos\Desktop>dir *.class
Volume in drive C is OS
Volume Serial Number is A45E-7B01
Directory of C:\Users\zobdos\Desktop
11/20/2010 10:16 AM 417 HelloWorld.class
1 File(s) 417 bytes
0 Dir(s) 8,145,432,576 bytes free
C:\Users\zobdos\Desktop>java HelloWorld
Exception in thread "main" 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)
Could not find the main class: HelloWorld. Program will exit.
C:\Users\btolbert\Desktop>
Nevermind, it works after using:
java -classpath . HelloWorld
You've a %CLASSPATH% environment variable in your environment. Get rid of it, it's disturbing your java commands, it's a poor practice tought by Sun Oracle anyway. Once you use -classpath argument or its -cp shorthand, then the %CLASSPATH% will be overridden. If the classpath is not specified by either the environment variable or the arguments, then the current path will be taken as default (as you initially expected).
run with classpath specified to the current directory:
java -cp . HelloWorld
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.)
I built a jar using IntelliJ, setting the main class properly.
When I run "java -jar foo.jar" from the command line (Windows), I get an exception that claims the main file is missing. The main class looks something like:
package mypackage;
public class LockUtil {
public static void main(String[] args) {
...
I'm getting the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: mypackage/LockUtil
Caused by: java.lang.ClassNotFoundException: mypackage.LockUtil
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)
Could not find the main class: mypackage.LockUtil. Program will exit.
The manifest file contains:
Manifest-Version: 1.0
Created-By: IntelliJ IDEA
Main-Class: mypackage.LockUtil
And the jar contains the appropriate directory structure with the .class file.
If you do jar -tf foo.jar, do you see something like this?
META-INF/
META-INF/MANIFEST.MF
mypackage/
mypackage/LockUtil.class
Could it be that there is another directory level in there somewhere?
You can be sure that Java knows the main file is there by building the jar file with something like this:
jar cfe foo.jar mypackage.LockUtil mypackage/LockUtil.class
You're trying to execute mypackage.LockUtil, but you should use mypackage.locking.LockUtil (note the package statement at the beginning of the class.).
Another possibility is that you have moved the class and forgot the update the package statement.
Does LockUtil have a dependency on another class that is not resolvable, thereby not allowing LockUtil to load?
It seems that the name of your package is mypackage.locking and not only mypackage
It appears that your main-class definition in your manifest is pointing at mypackage/LockUtil rather than mypackage/locking/LockUtil.
-Rick