Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a Java class in a package?

Tags:

java

I have two java classes as follows

App1 without a package:

class App1 {
    public static void main(String[] args) {
        System.out.println("App1 hello world...");
    }
}

App2 in a package:

package java.java.package1;    
class App2 {
    public static void main(String[] args) {
        System.out.println("App2 hello world...");
    }
}

I can compile them both:

D:\javaTest>javac App1.java

D:\javaTest>javac App2.java

However, I can only run the first one:

D:\javaTest>java App1
App1 hello world...

D:\javaTest>java java.java.package1.App2

Exception in thread "main" java.lang.NoClassDefFoundError: java/java/package1/App2
Caused by: java.lang.ClassNotFoundException: java.java.package1.App2
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: java.java.package1.App2.  Program will exit.

How can I run App2?

like image 982
gezhonglunta Avatar asked Jun 14 '12 11:06

gezhonglunta


People also ask

How is a class within a package compiled and executed?

Each component of the package name corresponds to a subdirectory. At the time of compilation, the compiler creates a different output file for each class, interface and enumeration defined in it. The base name of the output file is the name of the type, and its extension is . class.


2 Answers

If you put the source in an appropriate directory hierarchy matching the package name (D:\javaTest\java\java\package1\App1.java), and compile/run from the root of the hierarchy (D:\javaTest), you wouldn't have this problem:

D:\javaTest>javac java\java\package1\App1.java

D:\javaTest>java java.java.package1.App1
App2 hello world...

You can also compile using the -d option so that the classes are moved into such a directory hierarchy:

javac -d . App2.java
java java.java.package1.App2

Note you shouldn't use a package name starting with java, and later versions of the JDK will throw a SecurityException. See this question for more information.

like image 139
Jon Skeet Avatar answered Sep 20 '22 08:09

Jon Skeet


You create a new directory. This is the directory containing your work, and is not the start of your packages.

For instance, I create folder /terri to start.

I then create the folder structure /clarie/andrea under it. My package is going to be called claire.andrea in this example. Normal package names start with com and then a company name or something like that (or java for standard java packages, so don't use that: like java.lang.*).

In the andrea folder, I create a java file called Saluton.java with the class Saluton (which just print hello). The class name and the filename must match.

To compile, from the terri/ folder: javac .\claire\andrea\Saluton.java This will create a Saluton.class in the \terri\claire\andrea\Saluton.class

To run: (again from /terri), I do: java -cp . claire.andrea.Saluton Which says, use class path from my current directory.
My main program is in the package claire.andrea and the Class name is Saluton.

Here's the run: \terri java -cp . claire.andrea.Saluton

"Hello World".

To sum it up, the package name much match the underlying directory structure. The file (if it references a package) must live inside the directory stucture it is refering. If I compile Saluton.java in /terri with package claire.andrea I have not found a way to run it, it does compile fine.

Also, the filename for the class must match the public class in that file.

To run, package.Class. In general, packages are not capitalized and Classes are.

like image 34
Jerry Ludwig Avatar answered Sep 21 '22 08:09

Jerry Ludwig