This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Classpath in executing classes from command line  RSS feed

 
Ranch Hand
Posts: 77
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too struggled with this but am able to run from command line successfully after so much research on NoClassDefinition found error.

My question is - whenever a class has a package declared then is it necessary to add the package prefix to the class name for running that class? is it not possible to run the class without prefixing the package name? for javac it is working even without the package prefix.

eg:
javac className.java
java packageName.className

can anyone help me understanding how the commands "javac" and "java" search class files in the current directory ?

will it search in subdirectory also? when it is required to specify the current directory (".;") and when it is not required to specify in the classpath?

I am sorry if i'm asking questions that has been asked a thousand times already!
 
Marshal
Posts: 58378
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to use the java tool from the “root” of the package structure, and you must use the fully-qualified name of the class as an argument.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. javac does not check package structure but java command does. Hence your .class file should be present into folders resembling the package structure.
2. If you have not specified .; in your classpath environment variable then while running you have to specify java -cp .; to recognize current directory.
3. If you have no classpath environment variable set, then the current directory (.) will be taken as your default classpath.

If you search you will get good number of posts discussing this question.
 
Mohamed Iqzas
Ranch Hand
Posts: 77
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John. your comments are really useful.

but can you explain this alone - in what situations we run the class files without prefixing the package name before the class name, even though the class declared a package in it.

for example:

for the below file named MyClass.java:

package tries;

class MyClass
{
public static void main(String[] args)
{
System.out.println("Inside My Class!");

}

}

if i run like: java tries.MyClass
i get the output.

is it possible to run like: java MyClass
to produce the same output? i'm getting some error.

root of the package structure here is tries - if i'm right. even if i supply the "tries" path in the -classpath argument i'm not able to run it successfully.
 
Campbell Ritchie
Marshal
Posts: 58378
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohamed Iqzas wrote: . . . is it possible to run like: java MyClass
to produce the same output? i'm getting some error. . . .

Doesn’t that answer your question? I have already told you: you have to be in a particular directory to use the java command and you must use the full-qualified name of the class with the main method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!