简体   繁体   English

如何在命令行中运行 Java 项目

[英]How to run a Java project in command line

I have a Java project, which runs fine in Eclipse.我有一个 Java 项目,它在 Eclipse 中运行良好。 Right now, I need to run it using command line, like java classpath .现在,我需要使用命令行运行它,例如java classpath How do I setup the classpath based on the stored ones using in Eclipse?如何根据 Eclipse 中存储的类路径设置类路径?

Simply navigate to the directory that the class file exists in and use只需导航到 class 文件所在的目录并使用

java -classpath. myClass

Edit: You can replace the .编辑:您可以替换. with any classpath.与任何类路径。 For example, to find your classpath you can use echo %CLASSPATH%例如,要查找您的类路径,您可以使用echo %CLASSPATH%

Edit: Looks like there's quite a bit of information that might help you here .编辑:看起来有很多信息可以帮助

How to run a java project from command line using Runnable jar如何使用 Runnable jar 从命令行运行 java 项目

Using Eclipse you can easily run a java program but using Runnable jar is slightly different.使用 Eclipse 您可以轻松运行 java 程序,但使用 Runnable jar 略有不同。 Steps to run a java project:运行 java 项目的步骤:

  1. Export the java project in to a Runnable jar - using Eclipse IDE将 java 项目导出到可运行的 jar - 使用 Eclipse Z581D63872013F35E4FAC4FACD
  2. Select the main or running class file - Launch configuration Select 主要或运行 class 文件 - 启动配置
  3. In Library handling - select the option [ Extract required libraries in to jar file ]在库处理中 - select 选项 [将所需库提取到 jar 文件中]
  4. Open command prompt go to the directory where runnable jar is available打开命令提示符 go 到可运行 jar 可用的目录
  5. type > java -jar Runnable.jar类型 > java -jar Runnable.jar

Say you have changed directories to be in your project directory, and directly underneath that are a bin directory that has your compiled classes and a lib directory that has your jar files.假设您已将目录更改为位于项目目录中,并且在其正下方是包含已编译类的 bin 目录和包含 jar 文件的 lib 目录。 Also let's say the class with the main method you want to invoke is com.initech.example.EntryPoint.还假设您要调用的主要方法是 class 是 com.initech.example.EntryPoint。 On windows you can run:在 windows 上,您可以运行:

java -cp bin;lib\*.jar com.initech.example.EntryPoint

The slashes go the other way for Unix, obviously, and you use colon instead of semicolon as a separator in the cp switch.斜杠 go 显然是 Unix 的另一种方式,并且您在 cp 开关中使用冒号而不是分号作为分隔符。

Using -jar to run your project only works if your classes are packaged in a jar file (duh) and the jar file has a manifest that gives the entry point.仅当您的类被打包在 jar 文件 (duh) 中并且 jar 文件具有提供入口点的清单时,使用 -jar 来运行您的项目才有效。

Select the project and click on File->Export which opens a new window. Select 项目并单击文件-> 导出打开一个新的 window。

From that select Runnablejar option and click next button.从那个 select Runnablejar 选项,然后单击下一步按钮。

In launch configuration select your main class and in export destination give the path where you want to store the jar file.在启动配置 select 中,您的主要 class 和导出目标中提供您要存储 jar 文件的路径。

Now go the command line issue this command java -jar mainclass(classname)现在 go 命令行发出这个命令 java -jar mainclass(classname)

jre\bin\java -jar JarFileName.jar

This will allow you to run a jar on windows from command line.这将允许您从命令行在 windows 上运行 jar。

You can set the classpath using the -classpath or -cp option of the java command, or you can set the $CLASSPATH environment variable before launching your application, eg您可以使用java命令的-classpath-cp选项设置类路径,或者您可以在启动应用程序之前设置$CLASSPATH环境变量,例如

export CLASSPATH=bin:lib/*
java -jar packagename.Application

Either way you should probably write a script so that you can start the application without multiple steps or typing a long command.无论哪种方式,您都应该编写一个脚本,以便无需多个步骤或输入长命令即可启动应用程序。 Or you could use ant , or export a runnable Jar from Eclipse.或者您可以使用ant ,或从 Eclipse 导出可运行的 Jar。 It really depends why you need to do this.这真的取决于你为什么需要这样做。

To find what entries are needed in the classpath, it is probably simplest just to look at the .classpath file created by Eclipse in the root directory of the project.要查找类路径中需要哪些条目,最简单的方法可能是查看项目根目录中由 Eclipse 创建的.classpath文件。

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

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