2

I've got a .class file in my local directory, assembled from following jasmin code:

.class public default_class
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
.limit locals 10
.limit stack 10
invokestatic main_65428301()I
return
.end method
.method public static main_65428301()I
.limit locals 10
.limit stack 10
ldc "foobar"
astore_0
iconst_0
ireturn
.end method

when I do java -jar jasmin.jar test.j everything compiles and I get .class file, but I'm not able to start it with java default_class, because I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: 
    at default_class.main(test.j)
Caused by: java.lang.ClassNotFoundException: 
    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)
    ... 1 more

My JAVA_HOME is:

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

and classpath contains only my current directory (where this .class file is located). Should I add something to it?

EDIT: command I use to launch my class is java default_class

EDIT2: OK, finally I have figured out what was wrong! The name of the class has to start with uppercase! This solved my problem! Anyway, thanks to all of you who attempted to help me!

k_wisniewski
  • 2,439
  • 3
  • 24
  • 31

2 Answers2

2

You would generally want:

java -cp . NameOfClassWithMainMethod

to run a single class file sitting in the current directory. However, if you read your stack trace carefully, you will see that Java has found your main class and main method, but can't find something else. From the code you posted, I can't see what.

Running

java -verbose:class ......

might illuminate the situation, as might using as debugger to observe the class loader at work and play. As might using strace to see what files it is trying to open.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
0

Your error message says:

java.lang.ClassNotFoundException:

But the format of this message usually looks like this:

java.lang.ClassNotFoundException: SomeNonExistingClass

How comes that the name of the class that is not found is missing? Because you have an instruction in your example that refers to an empty class name (""). So you need to look for an instruction where you forgot to specify the class name. And that's here:

invokestatic main_65428301()I

Instead it should read:

invokestatic default_class/main_65428301()I

this will fix your problem.

It is suspect that you write:

EDIT2: OK, finally I have figured out what was wrong! The name of the class has to start with uppercase! This solved my problem! Anyway, thanks to all of you who attempted to help me!

Because neither is there any sign that this might be a cause, nor is this actually the case.

yankee
  • 38,872
  • 15
  • 103
  • 162