如何使用 javac 编译 Netbeans 项目
我正在用netbeans编写一个项目,但我需要用javac编译它,因为它需要通过CLI构建和执行。我怎样才能做到这一点?我看过很多特定于项目的答案,但没有一个适合我的问题。我已经阅读了 javac 的 man,并正确设置了类路径:javac -cp .. MyMain.java
,考虑到我的项目具有以下结构:
src
|_mainPackage
|_package2
|_package3
|_MyMain.java
并且我在导入中使用点格式,例如this:
import package1.class1
import package2.class2
public class MyMain {
public static void main(String[] args) {
//Stuff with classes in package1
.
.
.
//Stuff with classes in package2
.
.
.
}
}
我不能使用ant,因为它是在CLI中使用javac编译的,因为它是一个大学项目并且有教授施加的一些限制。
编辑: 实际上,我必须使用 javac 和命令 usr@host: mainPackage $ javac -cp .. MyMain.java
来编译我的源代码,因为我的导入使用点分隔格式,并且“根包”位于在“mainPackage”文件夹中;但是当我要运行该项目时,我有以下输出:
usr@host: mainPackage $ java VideoRent
Exception in thread "main" java.lang.NoClassDefFoundError: MyMain (wrong name: mymain/MyMain)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: MyMain. Program will exit.
I'm writing a project in netbeans, but I need it to be compiled with javac, because it is needed to be built and executed through CLI. How can I do that? I've seen a lot of project specific answers, but none suits my problem. I've read javac's man, and setted the classpath properly: javac -cp .. MyMain.java
, conssidering that my project have the following structure:
src
|_mainPackage
|_package2
|_package3
|_MyMain.java
and I'm using the dot format in my imports like this:
import package1.class1
import package2.class2
public class MyMain {
public static void main(String[] args) {
//Stuff with classes in package1
.
.
.
//Stuff with classes in package2
.
.
.
}
}
I can't use ant, because it is meant to be compiled using javac in the CLI, since it is a college project and have some restrictions imposed by the professors.
EDIT:
I actually got to compile my sources using javac with the command usr@host: mainPackage $ javac -cp .. MyMain.java
, since my imports use dot separation format, and the "root package" is located in 'mainPackage' folder; but when i'm going to run the project, i have this output:
usr@host: mainPackage $ java VideoRent
Exception in thread "main" java.lang.NoClassDefFoundError: MyMain (wrong name: mymain/MyMain)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: MyMain. Program will exit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在干净的临时目录中,尝试以下命令序列。然后检查并了解目录布局、包声明和各种路径设置,例如
-d bin
、-s src
和-cp bin
:In a clean temporary directory, try this sequence of commands. Then inspect and understand the directory layout, package declarations, and various path settings like
-d bin
,-s src
, and-cp bin
: