Java 如何在 Eclipse 中运行 jar 文件

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

How to run a jar file in Eclipse

javaeclipse

提问by stevebot

I tried to run a jar file by going to Run configurations and then creating a new Java App, but Eclipse wants me to give it a main class and reference to the project. Can't I just give Eclipse a jar file?

我试图通过转到运行配置然后创建一个新的 Java 应用程序来运行一个 jar 文件,但是 Eclipse 希望我给它一个主类和对项目的引用。我不能只给 Eclipse 一个 jar 文件吗?

采纳答案by Isaac Truett

You could run the JAR with an "External Tool" launch configuration, found at Run > External Tools > External tools Configurations.

您可以使用“外部工具”启动配置运行 JAR,该配置可在运行 > 外部工具 > 外部工具配置中找到。

回答by Vince

Is the jar file you provided have "public static void main(String [] arg)" method ? In order for java to run your and application, a main class with a "public static void main(String [] arg)" method must exist. So that jvm will understand the starting execution point of your jar.

您提供的 jar 文件是否具有“public static void main(String [] arg)”方法?为了让 Java 运行您的应用程序,必须存在具有“public static void main(String [] arg)”方法的主类。这样 jvm 就会理解你的 jar 的起始执行点。

回答by j040p3d20

Try the following command line

试试下面的命令行

C:\Program Files (x86)\Java\jdk1.6.0_24\bin\javaw.exe

with the following arguments (one per line)

带有以下参数(每行一个)

-cp C:\dev\tcpmon-1.0-bin\build\tcpmon-1.0.jar 
org.apache.ws.commons.tcpmon.TCPMon

回答by GermanK

You cannot directly execute a jar file. Instead, you need to run it using the java -jarcommand. In order to do that you:

您不能直接执行 jar 文件。相反,您需要使用java -jar命令运行它。为了做到这一点,你:

  1. Go to Run > External Tools > External tools Configurations
  2. Create an item under "Program"
  3. Fill in:

    Location: /usr/bin/java(or the route to your java executable, in Linux you can find this by running which javaon a terminal, in Windows you can use where javain command prompt)

    Working directory: The working directory for your jar program (usually its location)

    Arguments: -jar [path to your jar]

  1. 转到运行 > 外部工具 > 外部工具配置
  2. 在“程序”下创建一个项目
  3. 填写:

    位置:(/usr/bin/java或到您的 java 可执行文件的路径,在 Linux 中您可以通过which java在终端上运行来找到它,在 Windows 中您可以where java在命令提示符下使用)

    工作目录:jar 程序的工作目录(通常是它的位置)

    参数: -jar [path to your jar]

回答by TastyCode

Run -> External Tools -> External Tools Configuration.

运行 -> 外部工具 -> 外部工具配置。

Program -> New

程序 -> 新建

enter image description here

在此处输入图片说明

回答by Robert H?glund

Just create a Java application and start it with the jar flag:

只需创建一个 Java 应用程序并使用 jar 标志启动它:

enter image description here

在此处输入图片说明

You can use eclipse variables such as workspace_loc or git_work_tree to point to your jar-file.

您可以使用 eclipse 变量(例如 workspace_loc 或 git_work_tree)来指向您的 jar 文件。

回答by Gabriel Angelo Parcon

Try running 'java -jar yourprogramname' in a console.

尝试在控制台中运行“java -jar yourprogramname”。