如何编译包中的类,以便稍后使用“java 程序”(没有包名)执行它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4910964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:35:29  来源:igfitidea点击:

How can I compile classes in packages, to execute them later with "java Program" (without the package name)?

javapackagejavacnoclassdeffounderror

提问by FlamesPuck12

I have a quick question regarding javac and packages in Java.

我有一个关于 Java 中的 javac 和包的快速问题。

I have a simple program (we'll call it Program.java) which is currently in the following directory:

我有一个简单的程序(我们称之为 Program.java),它当前位于以下目录中:

  • myRepository/myProgram
  • 我的存储库/我的程序

In Program.java and other .java files in the myRepository/myProgram directory, I have declared package myProgram.*and also included import myProgram.*;.

在 myRepository/myProgram 目录中的 Program.java 和其他 .java 文件中,我已经声明package myProgram.*并包含了import myProgram.*;.

So when I type javac myProgram/Program.java, it compiles fine and it runs fine if I type java myProgram/Program.

因此,当我键入时javac myProgram/Program.java,它可以正常编译并且如果我键入它也可以正常运行java myProgram/Program

However, I'm trying to get the .class files to be produced in the myRepositorydirectory, not myRepository/myProgram, which is where the source files are. I tried javac myProgram/Program.java -d ..which produces the .class files in myRepository directory, but when I try "java Program", it gives me the following error:

但是,我试图在myRepository目录中生成 .class 文件,而不是myRepository/myProgram在源文件所在的目录中生成 .class 文件。我尝试javac myProgram/Program.java -d ..在 myRepository 目录中生成 .class 文件,但是当我尝试“java Program”时,它给了我以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: Program (wrong name: myProgram/Program).

线程“main”中的异常java.lang.NoClassDefFoundError:程序(错误名称:myProgram/Program)。

Is there any way way I could get .class files to show up in the main directory (myRepository) instead of where the source codes are (myRepository/myProgram) and be able to execute java Programwhile inside myRepository?

有什么方法可以让 .class 文件显示在主目录 (myRepository) 而不是源代码所在的位置 (myRepository/myProgram) 并且能够java Program在 myRepository 中执行?

回答by Dielson Sales

You better put the source codes on the directory source/myProgram and create a directory called build to put the .class files. Thus, you can compile and run this way:

您最好将源代码放在目录 source/myProgram 并创建一个名为 build 的目录来放置 .class 文件。因此,您可以这样编译和运行:

javac source/myProgram/Program.java -d build
cd build
java myProgram/Program

回答by vinga

Packaged classes can not be stored in any directory. This link could be helpful: http://www.herongyang.com/Java-Tools/javac-Option-d-Specifying-Output-Directory.html

打包的类不能存储在任何目录中。此链接可能会有所帮助:http: //www.herongyang.com/Java-Tools/javac-Option-d-Specifying-Output-Directory.html

回答by Pa?lo Ebermann

You should call the compiler from the myRepositorydirectory, not from inside the package directory, like this: javac myProgram/Program.java, and then java myProgram.Program.

您应该从myRepository目录中调用编译器,而不是从包目录中调用,如下所示:javac myProgram/Program.java,然后java myProgram.Program.

The created classes have to be in a package-structure for the classloader to find them (at least with the default classloader).

创建的类必须在一个包结构中,以便类加载器找到它们(至少使用默认的类加载器)。

You can put them in another directory, but then they will have the right structure there, and you should give this directory to your VM:

您可以将它们放在另一个目录中,但是它们将在那里具有正确的结构,您应该将此目录提供给您的 VM:

javac -d ../classes myProgram/Program.java
java -cp ../classes myProgram.Program

There is no way (with the default javacommand) to execute a class in a package without mentioning the package name.

没有办法(使用默认java命令)在不提及包名称的情况下执行包中的类。

You could use a wrapping class which is in the anonymous package (e.g. without any packagedeclaration), which simply calls the original class:

您可以使用匿名包中的包装类(例如,没有任何package声明),它只是调用原始类:

class Program {
   public static void main(String[] args) {
       // delegate to real main class and method:
       myProgram.Program.main(args);
   }
}

Alternatively, if you are developing with an IDE, you can often simply start your program with a button click.

或者,如果您使用 IDE 进行开发,通常只需单击按钮即可启动程序。

For distribution, you would instead put all your class files in a jar file and define the main class in the manifest. Then you can call your program this way:

对于分发,您可以将所有类文件放在一个 jar 文件中,并在清单中定义主类。然后你可以这样调用你的程序:

java -jar myProgram.jar