如何在 Java 中编译 .java 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2279451/
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-08-13 05:36:56  来源:igfitidea点击:

How to compile a .java file in Java?

javacompilationpackages

提问by Roman

I have the following code generated by Eclipse (.java file).

我有以下由 Eclipse 生成的代码(.java 文件)。

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Display;

public class HelloWorldSWT {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Hello world!");
        shell.open();
        while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }

}

Now I want to compile the above file from the command line. I went to the directory where the source code is located and I tried two commands:
1. javac HelloWorldSWT.java
2. javac -d /home/myname/workspace/ HelloWorldSWT.java

现在我想从命令行编译上面的文件。我去了源代码所在的目录,尝试了两个命令:
1. javac HelloWorldSWT.java
2. javac -d /home/myname/workspace/ HelloWorldSWT.java

In both cases I have the same error "The import org.eclipse cannot be resolved". /home/myname/workspace/ - is the directory where the class file is located.

在这两种情况下,我都有相同的错误“无法解析导入 org.eclipse”。/home/myname/workspace/ - 是类文件所在的目录。

As far as I understand the compiler does not see the org.eclipse.swt package. Why?

据我了解,编译器没有看到 org.eclipse.swt 包。为什么?

Can it be because the problematic package is located in "/home/myname/workspace/org.eclipse.swt/" (not in "/home/myname/workspace/org/eclipse/swt/")?

可能是因为有问题的包位于“/home/myname/workspace/org.eclipse.swt/”(而不是“/home/myname/workspace/org/eclipse/swt/”)?

采纳答案by benzado

You need to set your classpathso that the Java compiler knows where to find the org.eclipse.* classes. You can do that with a command line switch or an environment variable.

您需要设置类路径,以便 Java 编译器知道在哪里可以找到 org.eclipse.* 类。您可以使用命令行开关或环境变量来做到这一点。

回答by Thilo

Since you are doing Eclipse RCP development, you should let Eclipse handle your compilation as well. (You will most likely find your classes in a "build" or "bin" directory in the project). In addition to compilation, there will be some "packaging" steps to create the final application, and Eclipse has tools for that, too.

由于您正在进行 Eclipse RCP 开发,因此您也应该让 Eclipse 处理您的编译。(您很可能会在项目的“build”或“bin”目录中找到您的类)。除了编译之外,还有一些“打包”步骤来创建最终的应用程序,Eclipse 也有用于此的工具。

If you really want to build outside of Eclipse, you need to manage a potentially large list of dependencies (such as org.eclipse.swt.widgets), which makes a pure javac unfeasible. You would need to look at Ant or Maven.

如果你真的想在 Eclipse 之外构建,你需要管理一个可能很大的依赖项列表(例如 org.eclipse.swt.widgets),这使得纯 javac 不可行。您需要查看 Ant 或 Maven。

Also note that you will need the classpath to include dependencies not only for compilation, but also when you run the program.

另请注意,您将需要类路径来包含依赖项,不仅用于编译,还需要在运行程序时。

回答by St.Shadow

You forgot about classpath

你忘记了类路径

回答by deleted

But I though that I specify the "classpath" during the compilation (using -d option). I though that after the "-d" option I put the name of directory where all my packages are located. Do I understand that wrongly?

但是我虽然在编译期间指定了“类路径”(使用 -d 选项)。我虽然在“-d”选项之后放置了所有包所在的目录的名称。我理解错了吗?

try

尝试

javac -help

to see what the different command line options do. also note the other post above that explains this.

查看不同命令行选项的作用。还要注意上面解释了这一点的另一篇文章。

compiling from the command line and setting up classpath and everything right is a pain. however, it is useful to do it so that you understand what the ide actually does when it automates this for you.

从命令行编译并设置类路径,一切正确都是一件痛苦的事情。但是,这样做很有用,这样您就可以了解 ide 在为您自动执行此操作时实际执行的操作。

回答by James B

The classpath variable or command line switch needs to point to where the org.eclipse.swt.widgets.Shellclass resides, if this class is inside a jar file, then the classpath needs to contain the actual jar file,

classpath 变量或命令行开关需要指向org.eclipse.swt.widgets.Shell类所在的位置,如果这个类在 jar 文件中,那么 classpath 需要包含实际的 jar 文件,

i.e. javac -classpath /root/to/jar/eclipse.jar

IE javac -classpath /root/to/jar/eclipse.jar

Otherwise, if the org.eclipse.swt.widgets.Shell class is just a loose class file (which I doubt, I assume it will be inside one of the eclipse jar files, which you can list using jar -tvf jar-you-think-it-might-be-in.jar)...then you will need the javac -classpath to point to the location of the top level directory within the org/eclipse/swt/widgets/path.

否则,如果 org.eclipse.swt.widgets.Shell 类只是一个松散的类文件(我怀疑,我认为它会在一个 eclipse jar 文件中,您可以使用 列出jar -tvf jar-you-think-it-might-be-in.jar)...那么您将需要 javac -classpath 指向org/eclipse/swt/widgets/路径中顶级目录的位置。

回答by Stephen C

@Roman - this problem is too complicated for a beginner to try to address. The problem is that SWT has complicated dependencies, including dependencies on native code libraries.

@Roman - 这个问题对于初学者来说太复杂了,无法解决。问题在于 SWT 具有复杂的依赖关系,包括对本机代码库的依赖关系。

You are best off running your SWT application using Eclipse "RunAs" ... or trying to find some Eclipse-specific documentation on running SWT-based applications from the command line.

您最好使用 Eclipse“RunAs”运行您的 SWT 应用程序……或者尝试查找一些关于从命令行运行基于 SWT 的应用程序的特定于 Eclipse 的文档。

回答by St.Shadow

Ok, Stephen CI did this job by hand. I used only Notepad++ (I promise)

好的, 斯蒂芬 C我手工完成了这项工作。我只用过 Notepad++(我保证)

  1. Start Notepad++ and create file HelloWorldSWT.java
  2. Copy example from author
  3. Save it!
  4. Open cmd and go to the directory with HelloWorldSWT.java
  5. Run the command javac HelloWorldSWT.java

  6. Ok, go to the Eclipse directory and find the correct jar swt-3.4.2-win32-win32-x86.jar

  7. Run this again

    D:\workspaces\spf_workspace\hand-made>javac -cp "D:\Program files\eclipse3_5\plugins\org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar" HelloWorldSWT.java

  1. 启动 Notepad++ 并创建文件 HelloWorldSWT.java
  2. 复制作者的例子
  3. 保存!
  4. 打开cmd,进入HelloWorldSWT.java所在目录
  5. 运行命令 javac HelloWorldSWT.java

  6. 好的,进入Eclipse目录,找到正确的jar swt-3.4.2-win32-win32-x86.jar

  7. 再次运行这个

    D:\workspaces\spf_workspace\hand-made>javac -cp "D:\Program files\eclipse3_5\plugins\org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar" HelloWorldSWT.java

All process take 2 minutes.

所有过程需要 2 分钟。

Don't try to run this:

不要尝试运行这个:

`D:\workspaces\spf_workspace\hand-made>java -cp "D:\Program files\eclipse3_5\plugins\org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar;." HelloWorldSWT`

Note: I add current dir . to classpath too.

注意:我添加了 current dir 。也到类路径。