Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Invoking "java" in WEB-INF/classes

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!
Java Cowboy
Posts: 16084
Android Scala IntelliJ IDE Spring Java
  • 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
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.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic