简体   繁体   English

Java - 从命令行构建并运行eclipse项目

[英]Java - Build and run eclipse project from command line

I have a java project written using eclipse ide and I want to run it through ssh on a different machine, but I have to do this using the command line and I don't know exactly how. 我有一个使用eclipse ide编写的java项目,我想在不同的机器上通过ssh运行它,但是我必须使用命令行来完成这个,我不知道具体如何。

I am a beginner at both shell commands and java. 我是shell命令和java的初学者。

Could you please give me a useful link with answers regarding this question, or perhaps a set instructions of how to do this? 你能否给我一个有用的链接,提供有关这个问题的答案,或者可能是如何做到这一点的一套说明?

Maven or Ant are the best option but for an Eclipse-only solution Maven或Ant是最佳选择,但仅限Eclipse解决方案
you can choose File -> Export and select Java -> Runnable JAR File 您可以选择File - > Export并选择Java - > Runnable JAR File
then transfer the JAR file to your other machine and run this from the command line: 然后将JAR文件传输到另一台机器并从命令行运行:

java -jar YOUR.JAR

You can run Java applications from the command line. 您可以从命令行运行Java应用程序。 The simplified syntax looks like this: 简化的语法如下所示:

java -cp <classpath> <main class> <args>

where: 哪里:

<classpath> - list of directories and/or JAR-files where needed classes reside separated by ";" <classpath> - 所需类通过“;”分隔的目录和/或JAR文件列表 for Windows or ":" for linux (default classpath is "." - the current directory); 对于Windows或“:”对于linux(默认类路径为“。” - 当前目录);

<main class> - fully qualified name of the class containig main() method (for example, org.myself.HelloWorld) <main class> - 类containsig main()方法的完全限定名称(例如,org.myself.HelloWorld)

<args> - various arguments for application if any. <args> - 应用程序的各种参数(如果有)。

So, if you find the directory where Eclipse stored compiled classes (usually it's bin) you may use the command, like 因此,如果您找到Eclipse存储编译类的目录(通常是bin),您可以使用命令,例如

java -cp . my.package.MyClass

Or, if you use some libraries and classes in other directories, it could be: 或者,如果您在其他目录中使用某些库和类,则可能是:

java -cp some-cool-lib.jar:another-lib.jar:/some/directory/with/classes my.package.MyClass

To build and run a Java project, Its good to use an ant or maven tool. 要构建和运行Java项目,最好使用ant或maven工具。 you can find many tutorials on google for the same. 你可以在google上找到很多相同的教程。

a good tutorial on ant is here http://www.intranetjournal.com/java-ant/ 关于ant的一个很好的教程是http://www.intranetjournal.com/java-ant/

This is what I did and it worked for me. 这就是我所做的,它对我有用。 Hope it could help. 希望它可以帮助。 在此输入图像描述

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

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