简体   繁体   English

如何在Mac上使用终端从Eclipse编译Java程序

[英]How to compile Java programs from Eclipse using terminal on a Mac

I am trying to learn how to compile Java programs made on Eclipse, an IDE, in Terminal. 我正在尝试学习如何在Terminal中编译在Eclipse(IDE)上制作的Java程序。 Starting out right after opening terminal, what are the steps I should take to compile a program I've made on Eclipse in terminal? 打开终端后立即开始,在终端中编译在Eclipse上编写的程序应该采取什么步骤? Thanks for the help. 谢谢您的帮助。

UPDATE: I got as far as navigating to my java folder, and to the package that houses my programs, and did the line javac Hello.java (Hello is the basic "Hello World" program I'm trying to compile) but when I do java Hello I get a large error: 更新:我可以导航到我的java文件夹,以及存放我的程序的软件包,并执行javac Hello.java行(Hello是我要编译的基本“ Hello World”程序),但是当我Java你好我得到一个大错误:

Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: homeWorkPackage/Hello) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482) 线程“主”中的异常java.lang.NoClassDefFoundError:Java的java.lang.ClassLoader.defineClass1(本机方法)处的Hello(错误名称:homeWorkPackage / Hello)在Java处的java.lang.ClassLoader.defineClass1(本机方法) .security.SecureClassLoader.defineClass(SecureClassLoader.java:142)at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)at java.net.URLClassLoader.access $ 100(URLClassLoader.java:71)at java.net.URLClassLoader java.net.URLClassLoader上的$ 1.run(URLClassLoader.java:361)java.net.URLClassLoader.findClass(URLClassLoader.java)上java.security.AccessController.doPrivileged(Native Method)的$ 1.run(URLClassLoader.java:355) :354)在java.lang.ClassLoader.loadClass(ClassLoader.java:425)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)在java.lang.ClassLoader.loadClass(ClassLoader.java:358)在sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

What did I do wrong? 我做错了什么?

According to the official tutorials (assuming you have the JDK properly configured: 根据官方教程 (假设您已经正确配置了JDK:

  • Change the directory to the directory where your file is saved (using the cd command) 将目录更改为保存文件的目录(使用cd命令)
  • Use the command javac [filename.java] to compile the program 使用命令javac [filename.java]编译程序
  • there should be a class file in the directory now 现在目录中应该有一个类文件

If you are using eclipse, compiling from the terminal is not needed, however you can do it like this: 如果您使用的是eclipse,则不需要从终端进行编译,但是您可以这样进行:

Navigate to the project directory containing the .java files. 导航到包含.java文件的项目目录。 You can then run javac ClassName.java eg javac Cake.java javac is the Java language compiler. 然后,您可以运行javac ClassName.java例如javac Cake.java javac是Java语言编译器。 This command will compile the source (your .java file). 此命令将编译源代码(您的.java文件)。 To run it you can go java ClassName . 要运行它,您可以转到java ClassName eg java Cake . 例如java Cake java starts the JVM . java启动JVM The named class will be loaded and execution started. 命名的类将被加载并开始执行。 You don't include the .class file extension one the java ClassName command. 您不要在java ClassName命令中包括.class文件扩展名。

When you need you navigate around the file system I think this page gives a good overview of the commands but here are a few that you might need for this task: 当您需要浏览文件系统时,我认为页面对命令进行了很好的概述,但是此任务可能需要一些命令:

cd - change directory (followed by directory name) eg cd Documents cd更改目录(后跟目录名称),例如cd Documents

ls - list information about files (can take some parameters) ls列出有关文件的信息(可以使用一些参数)

.. can take you back a directory. ..可以带您回到目录。 eg cd .. will bump you back one directory 例如cd ..将使您返回一个目录

you can also hit tab to auto complete a directory/file name. 您还可以点击tab以自动完成目录/文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM