Error when running helloworld + classpath problem - java

I am compiling helloworldapp with no errors.
I am running
java helloworldapp
on windows
CLASSPATH:
.;C:\Program Files\Java\jre7\lib;
JAVA_HOME:
C:\Program Files\Java\jre7
path includes
C:\Program Files\Java\jdk1.7.0\bin\
Result:
C:\Users\k\Desktop\abcl-bin-0.26.2>java helloworldapp
Exception in thread "main" java.lang.NoClassDefFoundError: helloworldapp (wrong
name: HelloWorldApp)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
............etc
So ok, for some reson it couldn't find the classes, so I am trying to do it manually.
C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp "C:\Program Files\Java\jre7\lib" helloworldapp
Error: Could not find or load main class helloworldapp
C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp C:\Program Files\Java\jre7\lib; helloworldapp
Error: Could not find or load main class Files\Java\jre7\lib;
What am I doing wrong?
I have looked at several forums, but they weren't particularly helpful.
Update, I got past the helloworld stage.
Now the real problem
import org.armedbear.lisp.*;
public class Main
{
public static void main(String[] argv)
{
try
{
Main thisObject = new Main();
Interpreter interpreter = Interpreter.createInstance();
interpreter.eval("(load \"lispfunctions.lisp\")");
org.armedbear.lisp.Package defaultPackage =
Packages.findPackage("CL-USER");
Symbol voidsym =
defaultPackage.findAccessibleSymbol("VOID-FUNCTION");
Function voidFunction = (Function) voidsym.getSymbolFunction();
voidFunction.execute(new JavaObject(thisObject));
}
catch (Throwable t)
{
System.out.println("exception!");
t.printStackTrace();
}
}
public int addTwoNumbers(int a, int b)
{
return a + b;
}
}
I tried both
C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp abcl.jar Main
Error: Could not find or load main class Main
C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp abcl.jar main
Error: Could not find or load main class main
The abcl.jar is located in the same folder
Some more trials and errors
C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp .:abcl.jar Main
Error: Could not find or load main class Main
C:\Users\k\Desktop\abcl-bin-0.26.2>java Main -cp .:abcl.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/armedbear/lisp/Li
spObject
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.armedbear.lisp.LispObject
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

When you write a class in java, the name of the file must match the name of the class.
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
must be saved as HelloWorldApp.java
It must match the case i.e. helloworldapp.java would not work.
When you need to run the class, you must refer to it using the correct case.
java HelloWorldApp
but not
java helloworldapp
If you are referencing a JAR then you need to add it to your classpath. By default the classpath consists of the files in the current working directory.
On windows the classpath is a semi-colon (;) separated list, on Linux and Mac OSX it is a colon (:) separated list.
To include the JAR in the classpath you must run the following...
java -cp .:myjar.jar MyMainClass
where myjar.jar is the JAR file you want to reference and MyMainClass is the class that contains your public static void main method.

Related

ClassNotFound exception in java command

I have simple Hello word program. Program will compile and run when not declare namespace in code but when I declared the class within namespace and compile the program it will complie successfully but it will given an error classnotfound when this program run. My question is why java complier not availble to find class when declare namespace (package) in code?
Please find below source code:
package org;
public class Chunk
{
public static void main(String [] args)
{
System.out.println("Hello, World");
}
}
and command is
java org.Chunk
and error is
java.lang.NoClassDefFoundError: org/Chunk
Caused by: java.lang.ClassNotFoundException: org.Chunk
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: org.Chunk. Program will exit.
Exception in thread "main"
You are getting this error probably because the class is not part of the classpath. You can specify a classpath using -cp java option, to point to the directory/jar where the org.Chunk class is found.

NoClassDefFoundError running java from the console

When I tried to run Java on Linux on the terminal this is what happens:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class
Caused by: java.lang.ClassNotFoundException: HelloWorldApp.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)
Can anyone help me with this?
EDIT: I was in the folder of the bytecode file and ran this on the terminal:
bash-4.1$ java class HelloWorldApp
Source file:
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
You are executing
java HelloWorldApp.class
but it must be
java HelloWorldApp
You may not append class to your call but name plain the classname.
Also, as others has remarked, it is better to use packages as classes in the default package do not work fine.
You also should note that if you have the package declaration in the code it will screw it up even if you try all the fancy fixes like setting CLASSPATH. For example if you have:
package blah;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
The line: package blah;
will cause java HelloWorld to fail after compiling. So remove this line and you should be able to run the src via cmd line.

EMMA [MAIN_METHOD_NOT_FOUND]

Am trying to use Emma for checking the code coverage of my application and
am trying to run like this
D:\bin>java -cp val.jar;emma.jar emmarun -r html -cp . com.val.ui.ValidatorClient
EMMA: no coverage data collected at runtime [all reports will be empty]
EMMA: writing [html] report to [D:\bin\coverage\index.html] ...
[EMMA v2.0, build 5312]
emmarun: [MAIN_METHOD_NOT_FOUND] application class [com.psft.sqlval.ui.Validator
Client] does not have a runnable public main() method
Exception in thread "main" com.vladium.emma.EMMARuntimeException: [MAIN_METHOD_N
OT_FOUND] application class [com.val.ui.ValidatorClient] does not have a
runnable public main() method
at com.vladium.emma.rt.AppRunner._run(AppRunner.java:497)
at com.vladium.emma.rt.AppRunner.run(AppRunner.java:97)
at com.vladium.emma.runCommand.run(runCommand.java:247)
at emmarun.main(emmarun.java:27)
Caused by: java.lang.ClassFormatError: Illegal local variable table length 18 in
method com.val.ui.ValidatorClient.main([Ljava/lang/String;)V
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 com.vladium.emma.rt.AppRunner._run(AppRunner.java:493)
... 3 more
But my class ValidatorClient.java has a valid main method
public class ValidatorClient
{
...
public static void main (String[] args) throws Exception
{
....
Can somebody throw some light on this?
Thanks in advance

Scanner no class error

I'm new to java and i have a problem while trying to run my program. I'm using eclipse.
import java.util.Scanner;
public class Scan {
public static void main(String[] args) {
String imie;
Scanner odczyt = new Scanner(System.in);
imie=odczyt.nextLine();
System.out.println("Witaj "+imie);
}}
This what I get:
EDIT(Runned without dot)
Exception in thread "main" java.lang.NoClassDefFoundError: Scan
Caused by: java.lang.ClassNotFoundException: Scan
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: Scan. Program will exit.
Please help me. :)
When you're running java, don't put .java on the end of the class name.
It looks like you're trying to run a class named Scan.java, but there is no such class; the class is just named Scan. However you're launching your class, you need to launch just Scan, i.e.,
java Scan
not
java Scan.java
You need to specify classpath that specifies where Scan class can be found. The classpath can point to a folder of classes or a JAR file. For instance...
java -cp scan.jar Scan

java classpath related query with import statement

My program below is called Apple.java
import food.Fruit;
class Apple extends Fruit{
public static void main(String[] args){
int y=20,z=0;
z=y+10;
System.out.println("x+y is "+z);
}
}
This is another program called Fruit.java
package food;
public abstract class Fruit{
public static void main(String[] args){
int x=10;
System.out.println("x is "+x);
}
}
Both these programs are stored in the following directory path :- G:\Java\food
I did successfully compile both the programs using the following command:-
G:\>javac Java\food\*.java
and it produced class files.
I did manage to execute the Fruit program using the following command:-
G:\>java -classpath G:\Java food.Fruit
However, when I tried to do the same for the Apple program it resulted in error messages.
G:\>java -classpath G:\Java food.Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Apple (wrong name: Apple)
Could someone please help me in figuring out what the error is? I am trying to get myself ready for the SCJP cert exam and hence I reference this example back to the McGrawHill publication book written by Kathy Sierra and Bert Bates.
Edit #2
Yes, I did because that was how the code was in the book. Is there a way I could still execute the file without having to include the package statement.
This is what the question looks like:
Attempt to compile the two files. If you want to use the Apple class, make
sure you place the Fruit.class file in the food subdirectory.
Edit #3
I did try that and got the following errors
G:\>java -classpath G:\Java\food Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Fruit
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)
Caused by: java.lang.ClassNotFoundException: food.Fruit
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)
... 12 more
Could not find the main class: Apple. Program will exit.
Edit #4
I did manage to execute it without having to include the statement
package food;
G:\>java -classpath G:\Java\food;G:\Java Apple
x+y is 30
What I changed was I moved the Apple.class file into a separate folder which makes sense because the folder "food" is an indication of the package and as Darvis pointed all files in the folder needed to have the package name else it would not execute.
Edit #5
This worked too
G:\>java -classpath G:\Java Apple
x+y is 30
because you haven't specified food package for that. make it like this and it will work
package food;
import food.Fruit;
class Apple extends Fruit{
public static void main(String[] args){
int y=20,z=0;
z=y+10;
System.out.println("x+y is "+z);
}
}
Update:
When you execute below
G:\>java -classpath G:\Java food.Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Apple (wrong name: Apple)
it will add G:\Java in class path from there it will relatively search for class apple in food DIR with package name specified food
Or. if you don't want to specify package in the class then
G:\>java -classpath G:\Java\food Apple

Categories