如何使用 javac 编译 Netbeans 项目

发布于 12-08 10:24 字数 1867 浏览 0 评论 0原文

我正在用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铜锣湾横着走2024-12-15 10:24:59

在干净的临时目录中,尝试以下命令序列。然后检查并了解目录布局、包声明和各种路径设置,例如 -d bin-s src-cp bin

mkdir -p src/mainPackage
echo "package mainPackage; public class MyMain { public static void main(String[] args) { System.out.println(\"Hello world\"); }}" > src/mainPackage/MyMain.java
mkdir bin
javac -d bin -s src src/mainPackage/MyMain.java 
java -cp bin mainPackage.MyMain

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:

mkdir -p src/mainPackage
echo "package mainPackage; public class MyMain { public static void main(String[] args) { System.out.println(\"Hello world\"); }}" > src/mainPackage/MyMain.java
mkdir bin
javac -d bin -s src src/mainPackage/MyMain.java 
java -cp bin mainPackage.MyMain
哆啦不做梦2024-12-15 10:24:59

试试这个:

javac.exe -d class -s src src/myproject/mainpackage/*.java 
      src/myproject/mainpackage/package2/*.java 
        src/myproject/mainpackage/package3/*.java

也可以尝试这个(Windows)

for /r %%a in (*.java) do (javac "%%a")

Try this:

javac.exe -d class -s src src/myproject/mainpackage/*.java 
      src/myproject/mainpackage/package2/*.java 
        src/myproject/mainpackage/package3/*.java

Could try this also (Windows)

for /r %%a in (*.java) do ( javac "%%a" )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文