如何在Java中编译.java文件? [英] How to compile a .java file in Java?
问题描述
我有以下生成的代码(.java文件)。
import org.eclipse.swt.widgets。贝壳;
import org.eclipse.swt.widgets.Display;
public class HelloWorldSWT {
/ **
* @param args
* /
public static void main(String [] args ){
// TODO自动生成的方法存根
显示display = new Display();
shell shell = new Shell(display);
shell.setText(Hello world!);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())display.sleep();
}
display.dispose();
}
}
现在我想编译上面的文件从命令行。我去了源代码所在的目录,我试过两个命令:
1。 javac HelloWorldSWT.java
2。 javac -d / home / myname / workspace / HelloWorldSWT.java
在这两种情况下,我都遇到了无法解析导入org.eclipse错误。
/ home / myname / workspace / - 是类文件所在的目录。
据我所知,编译器没有看到org。 eclipse.swt包。为什么?
可能是因为有问题的包位于/home/myname/workspace/org.eclipse.swt/(不在/ home / myname / work> 以便Java编译器知道在哪里找到org.eclipse。*类。您可以使用命令行开关或环境变量。
I have the following code generated by Eclipse (.java file).
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
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.
As far as I understand the compiler does not see the org.eclipse.swt package. Why?
Can it be because the problematic package is located in "/home/myname/workspace/org.eclipse.swt/" (not in "/home/myname/workspace/org/eclipse/swt/")?
You need to set your classpath so 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中编译.java文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!