• Post Reply Bookmark Topic Watch Topic
  • New Topic

Invoking "java" in WEB-INF/classes  RSS feed

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! I have a webapp that runs on a tomcat container. I have a standalone application inside it in WEB-INF/classes/dir1/dir2/dir3. Why is it that when I go to WEB-INF/classes/dir1/dir2/dir3 and then invoke

java MyClass

from that directory, I get a NoClassDefinitionFound? But when I invoke from WEB-INF/classes/

java dir.dir2.dir3/MyClass

it works? Why? Is there any way I can invoke MyClass from dir3? Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because your class is in the package dir1.dir2.dir3.

When you run a Java program that is in a package, you must make sure that the base directory of the package is in the classpath, and you have to specify the fully-qualified class name (i.e. the class name including the package name) on the command line.

By default (if you don't specify a classpath) Java will use the current directory. So it works if you are in the directory WEB-INF/classes, because that is the base directory of the package.

You can try specifying the classpath on the command line explicitly, then it doesn't matter from which directory you start it:

java -cp C:\myproject\...\WEB-INF\classes dir1.dir2.dir3.MyClass
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With other words, the name of the class is dir1.dir2.dir3.MyClass. This is also the called the "fully qualified name" of the class.

You are allowed to use the shortened name (without the package prefix) in Java code when you use an import statement. The same is not possible with the java command when you want to run the class - there you always need to use the fully qualified name.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!