如何编译包中的类,以便稍后使用“java 程序”执行它们? (没有包名)?
我有一个关于 Java 中的 javac 和包的快速问题。
我有一个简单的程序(我们将其称为 Program.java),当前位于以下目录中:
- myRepository/myProgram
在 Program.java 和 myRepository/myProgram 目录中的其他 .java 文件中,我已声明 package myProgram .*
还包括 import myProgram.*;
。
因此,当我输入 javac myProgram/Program.java
时,它可以正常编译,并且如果我输入 java myProgram/Program
,它也可以正常运行。
但是,我试图在 myRepository
目录中生成 .class 文件,而不是在 myRepository/myProgram
中生成源文件所在的位置。我尝试了 javac myProgram/Program.java -d .. ,它在 myRepository 目录中生成 .class 文件,但是当我尝试“java Program”时,它给出了以下错误:
线程“main”java.lang.NoClassDefFoundError中出现异常:程序(错误名称:myProgram/Program)。
有什么方法可以让 .class 文件显示在主目录(myRepository)中,而不是源代码所在的位置(myRepository/myProgram),并且能够在 myRepository 内执行 java Program
?
I have a quick question regarding javac and packages in Java.
I have a simple program (we'll call it Program.java) which is currently in the following directory:
- myRepository/myProgram
In Program.java and other .java files in the myRepository/myProgram directory, I have declared package myProgram.*
and also included import myProgram.*;
.
So when I type javac myProgram/Program.java
, it compiles fine and it runs fine if I type java myProgram/Program
.
However, I'm trying to get the .class files to be produced in the myRepository
directory, 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:
Exception in thread "main" java.lang.NoClassDefFoundError: Program (wrong name: 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 Program
while inside myRepository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您最好将源代码放在目录 source/myProgram 中,并创建一个名为 build 的目录来放置 .class 文件。因此,您可以这样编译和运行:
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:
打包的类不能存储在任何目录中。
此链接可能会有所帮助:
http://www.herongyang.com/Java -Tools/javac-Option-d-Specifying-Output-Directory.html
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
您应该从
myRepository
目录调用编译器,而不是从包目录内部调用,如下所示:javac myProgram/Program.java
,然后java myProgram.Program
。创建的类必须位于包结构中,以便类加载器找到它们(至少使用默认类加载器)。
您可以将它们放在另一个目录中,但是它们在那里将具有正确的结构,并且您应该将此目录提供给您的虚拟机:
没有办法(使用默认的 java 命令)来执行类在一个包中而不提及包名称。
您可以使用匿名包中的包装类(例如,没有任何
package
声明),它仅调用原始类:或者,如果您正在使用 IDE 进行开发,则通常可以简单地启动您的单击按钮即可进行编程。
为了进行分发,您可以将所有类文件放入一个 jar 文件中,并在清单中定义主类。然后你可以这样调用你的程序:
You should call the compiler from the
myRepository
directory, not from inside the package directory, like this:javac myProgram/Program.java
, and thenjava 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:
There is no way (with the default
java
command) to execute a class in a package without mentioning the package name.You could use a wrapping class which is in the anonymous package (e.g. without any
package
declaration), which simply calls the original class:Alternatively, if you are developing with an IDE, you can often simply start your program with a button click.
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: