How to run a .class file that is part of a package

2019-01-22 06:14发布

I keep getting errors when I make my .class part of a package and try to run it from cmd.

Here's the code that works after using javac and then java:

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

and then the code that does not work:

package com;

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

giving me this error after trying to run the program via the command: java HelloWorld:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong nam
e: com/HelloWorld)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    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)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Here's what I've tried doing so far:

java -cp . HelloWorld
java -cp . com.HelloWorld
java -cp . com/HelloWorld
java HelloWorld
java com.HelloWorld
java com/HelloWorld

Keep in mind that javac returns with no errors and that simply removing package com; solves the problem. Sometimes in other scenarios I get an error that says the main class file cannot be found or something along those lines.

What am I doing wrong?

11条回答
够拽才男人
2楼-- · 2019-01-22 06:33

There could be such a problem, when you are executing 'java' command directly from folder of your '*.class' files. This command should be executed from project 'parent' directory. All is well explained in the following article:

http://javarevisited.blogspot.de/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html

查看更多
对你真心纯属浪费
3楼-- · 2019-01-22 06:37

Suppose you did cd C:/projects and HelloWorld.class is in C:/projects/com, then just type:

java com.HelloWorld
查看更多
Rolldiameter
4楼-- · 2019-01-22 06:37

Create a folder named com under Java folder and put the HelloWorld.java into com folder. Then run again javac and java.

查看更多
Summer. ? 凉城
5楼-- · 2019-01-22 06:40

Run the program from the parent directory of the com directory.

java com.HelloWorld
查看更多
不美不萌又怎样
6楼-- · 2019-01-22 06:41

Try to use absolute directory or put your HelloWorld.class into ..\last_directory\com

1. java -cp .......\last_directory com.HelloWorld
2. java -cp .......\last_directory HelloWorld(with created com)
查看更多
祖国的老花朵
7楼-- · 2019-01-22 06:42

You do not need a -cp flag while running a java class , -cp needed while running a class or a main program from a binary (jar) file. While running a main program from a command line you need to make sure you have the class in the same folder structure as package name in the java file, ex.

home/user/foo java com.test.className   

here classNmae class file and exist under home/user/foo/com/test

hope it helps.

查看更多
登录 后发表回答