• Post Reply Bookmark Topic Watch Topic
  • New Topic

stuck between "wrong name" and NoClassDefFoundError  RSS feed

 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get the code here to run:

http://uadetector.sourceforge.net/usage.html

In order to get the code to compile I had to add some imports. My import section now looks like this:



My program source and the three jar files that come with the project are all in the same directory:



I include my tomcat's servlet-api.jar in my compile.



When I try to run the servlet code I get an error:



My thought is that I don't need to define a package so I comment out the first line of the program:



When I do that and recompile I get a different error.



I've read a bit about the "wrong name" error but I don't get how to fix this.

Help!

 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I change the package line to be this:



and then cd .. and recompile and run I get past the "wrong name" error but I still get the NoClassDefFoundError error.


This is confusing me because the java compiler obviously found the class in the jar file when it compiled the program. Obviously, I'm not understanding what's different about finding classes during compile time vs. during run time.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Lederer wrote:When I try to run the servlet code I get an error:

My thought is that I don't need to define a package so I comment out the first line of the program:

When I do that and recompile I get a different error.


The class is named HelloServlet in the net.sf.uadetector.example package. You are trying to run HelloServlet in the default package. Instead of changing the package and recompiling, isn't it easier to just run it with the correct package name (after moving it to the correct sub-directory)? Like so...

$ java net.sf.uadetector.example.HelloServlet

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Lederer wrote:
This is confusing me because the java compiler obviously found the class in the jar file when it compiled the program. Obviously, I'm not understanding what's different about finding classes during compile time vs. during run time.


The class files needed by the compiler, and the class files needed by the runtime system are different. The compiler only needs the classes that are used by the class that it is compiling, in order to resolve variables and method calls. It doesn't need the class file for the class that it is compiling (as it is generating that file). And it doesn't need the class files used by the class files that the main class is using.

You are likely missing a support jar file, that is needed by one of the classes in one of your jar files.

Henry
 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my uadetector dir I created net/sf/uadetector/example and moved HelloServlet.class there and then I recompiled:

$ javac -cp "uadetector-core-0.9.22.jar:uadetector-json-0.1.22.jar:uadetector-resources-2014.10.jar:/srv/apache-tomcat-7.0.52/lib/servlet-api.jar" net/sf/uadetector/example/HelloServlet.java

Then I tried to run it but same problem:

$ java net.sf.uadetector.example.HelloServlet
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)

You are likely missing a support jar file, that is needed by one of the classes in one of your jar files.

So, this question is boiling down to how can I figure out what jar file is missing? Is there a way to read the stacktrace and figure this out?

 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, I guess that a more fundamental question is, if I'm missing a jar file, and my code compiled fine without it, then how do I reference that jar file at run time? I use the -cp option at compile time to specify a jar file to compile with but there's no such mechanism for specifying a jar file at run time that I know.
 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... I set CLASSPATH to the set of paths that I used in the -cp option and I now get past that error. Interesting. I guess there is the notion of compile time and run time class paths. Thanks for your help, Henry.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Lederer wrote:And, I guess that a more fundamental question is, if I'm missing a jar file, and my code compiled fine without it, then how do I reference that jar file at run time? I use the -cp option at compile time to specify a jar file to compile with but there's no such mechanism for specifying a jar file at run time that I know.



Why can't you also use the "-cp" command line option at runtime? In fact, I just assumed that you had the CLASSPATH environment set, because you didn't specify it on the command line.

Henry
 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Now I know that CLASSPATH and -cp can both be used at build time and at run time. Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!