0

This is follow-up on my question:

error in java basic test program

This is still confusing me. So specific problem is:

I already have a package and directory done. say com.learn.java.mypackage

$ pwd
.../com/learn/java/mypackage

and here I want to create a test program in the same package and execute it.

$cat TestPackage.java

package com.learn.java.mypackage;

public class TestPackage
{

public static void main(String args[])
{
System.out.println("Hello World\n");
}
}

$ javac TestPackage.java
// runs file

$ Java TestPackage


Exception in thread "main" java.lang.NoClassDefFoundError: TestPackage (wrong name: com/learn/java/TestPackage)

if java runtime wants to make sure that a file belonging to a package lives in the same named directory, it is true here. Then why does it still crib?

Community
  • 1
  • 1
xyz
  • 8,607
  • 16
  • 66
  • 90

5 Answers5

1

Compilation would work fine in the same directory as the java file.
However, for running the java program, you need to go the directory where the package starts and provide the full package information to the command

Go to directory which has the com folder of you package and use

java com.learn.java.mypackage.TestPackage
Jayendra
  • 52,349
  • 4
  • 80
  • 90
  • this works..How can I make java running independent of current path..and avoid having to be necessarily in the starting path of the package – xyz Oct 05 '11 at 13:08
  • If you want it to be independant of the path, you need to add the directory (in which the com folder is) to the classpath. The class file would be accesible all over but still need the full package path to be executed. – Jayendra Oct 05 '11 at 13:10
1

You should do java com.learn.java.mypackage.TestPackage from within the parent folder of com.

So if com's full path is /my/project/folder/com/... you should call java com.learn.java.mypackage.TestPackage from /my/project/folder

Pasted in from the chat:

You can use from anywhere java -cp /my/project/folder a.b.c.ClassName, which tells java to look for class ClassName found in the packages a.b.c, and the packages should be searched at the path: /my/project/folder

Matyas
  • 13,473
  • 3
  • 60
  • 73
  • This works. Thanks. What does the command look like if I want to run my class independent of my current location? – xyz Oct 05 '11 at 13:10
  • use java -cp /my/project/folder com.learn.java.mypackage.TestPackage , where /my/project/folder is the parent of com – Matyas Oct 05 '11 at 13:12
  • so this is confusing..with TestPackage, we should give the whole package name..it is not that the class file embeds its package information in itself..and when I want to run the class, I just give the class name and from classpaths it will fetch the class prefixed by package path..?? There seems some redundancy in this approach.. some description/pointers with reason behind this approach should be useful.. – xyz Oct 05 '11 at 13:14
  • Also to fiddle around with this..what all ways exist to run a class belonging to a package without having to give the entire package name with the class..? – xyz Oct 05 '11 at 13:17
  • a class is identified by it's container package(s). so u can have a.b.c.ClassName, and e.f.g.ClassName . if you call `java` from the folder where `a` and `e` are in, like: `java a.b.c.ClassName`, it will know to search `./a/b/c/` for class with name `ClassName`. This is the default behaviour. As for the 2nd question, I don't really know a way to get it working by only calling `java ClassName`. Otherwise I would suggest to use [eclipse](http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/indigosr1) (download links on the right - or if on linux, check repo) – Matyas Oct 05 '11 at 13:17
  • given a class, is there a way to know which package does it belong to? – xyz Oct 05 '11 at 13:21
  • From outside the class (shell): No. (AFAIK) This is the standard way of starting a program. My suggestion is to use IDEs if it's a hassle for you. – Matyas Oct 05 '11 at 13:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4029/discussion-between-matyas-and-p2pnode) – Matyas Oct 05 '11 at 13:23
0

Add . to the classpath, and use the fully qualified name (with package)

java -cp . com.learn.java.mypackage.TestPackage

SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

The explanation is provided by this Java tutorial:

The full path to the classes directory, \classes, is called the class path, and is set with the CLASSPATH system variable. Both the compiler and the JVM construct the path to your .class files by adding the package name to the class path. For example, if

<path_two>\classes

is your class path, and the package name is

com.example.graphics, then the compiler and JVM look for .class files in

<path_two>\classes\com\example\graphics.

A class path may include several paths, separated by a semicolon (Windows) or colon (Unix). By default, the compiler and the JVM search the current directory and the JAR file containing the Java platform classes so that these directories are automatically in your class path.

JRL
  • 76,767
  • 18
  • 98
  • 146
0

You've to compile your file with the flag "-d" in this way:

javac -d . TestPackage.java java TestPackage.java

Try!

Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146