Java 命令行 Jar 文件

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

Java Command Line Jar File

javacommand-linejar

提问by Pandemic

I've created a few .jar files (using Eclipse's export feature), but they were all GUIs. I want to create a .jar file that is strictly command-line; is this possible?

我已经创建了一些 .jar 文件(使用 Eclipse 的导出功能),但它们都是 GUI。我想创建一个严格命令行的 .jar 文件;这可能吗?

回答by Gregory Pakosz

Yes it's possible to create a console program in Java. Eclipse even has a Runnable JAR File Export.

是的,可以用 Java 创建一个控制台程序。Eclipse 甚至有一个 Runnable JAR 文件导出。

I just created a dummy application that prints out the arguments given on the command line, then exported it as a jar and launched it using java -jar test.jar foo bar

我刚刚创建了一个虚拟应用程序,它打印出命令行上给出的参数,然后将其导出为 jar 并使用 java -jar test.jar foo bar

package com.stackoverflow;

public class Test
{
  public static void main(String[] args)
  {
    for (int i = 0; i < args.length; ++i)
      System.out.println(args[i]);
  }
}

Then right click the Test.javafile in Eclipse and use the "Export..." menu and select "Runnable JAR file" and export it for instance to C:\test.jar

然后Test.java在 Eclipse 中右键单击该文件并使用“导出...”菜单并选择“可运行 JAR 文件”并将其导出到例如C:\test.jar

Then open a command prompt and type java -jar C:\test.jar foo barwhich prints out

然后打开命令提示符并键入java -jar C:\test.jar foo bar打印出来的

foo
bar


回答by Paul Tomblin

There is no such thing as a "GUI jar" or a "command line jar". If you want a program to work on the command line, then you read and write the from System.in and System.out or files rather than using Swing or AWT.

没有“GUI jar”或“命令行 jar”之类的东西。如果您希望程序在命令行上工作,那么您可以从 System.in 和 System.out 或文件中读取和写入,而不是使用 Swing 或 AWT。

If on the other hand you are asking how to create a jar on the command line, the command is, not too surprisingly "jar". As in jar cf foo.jar com/my/foo/*.class

另一方面,如果您问如何在命令行上创建 jar,那么命令是“jar”,这并不奇怪。如jar cf foo.jar com/my/foo/*.class

回答by MattGrommes

Yes, you can run your program with something similar to the following command-line

是的,您可以使用类似于以下命令行的内容运行您的程序

java -jar <JARFILE> <CLASSNAME>

You may have to specify -cp <DIRECTORY>or -classpath <DIRECTORY>to point it at any other jars you need.

您可能必须指定-cp <DIRECTORY>-classpath <DIRECTORY>将其指向您需要的任何其他 jar。

回答by Alex Ntousias

I'm not sure what you're asking, but all depends on your code.

我不确定你在问什么,但这一切都取决于你的代码。

If your Java code uses JFrame and other GUI components then yes, when you create the jar you will have a GUI program.

如果您的 Java 代码使用 JFrame 和其他 GUI 组件,那么是的,当您创建 jar 时,您将拥有一个 GUI 程序。

If your Java code just prints on the screen without using any GUI components, then you'll have a console application.

如果您的 Java 代码只是在屏幕上打印而不使用任何 GUI 组件,那么您将拥有一个控制台应用程序。

回答by stacker

To automate things I would recommend using the Ant jartask. An Ant buildfile looks like this:

为了使事情自动化,我建议使用Antjar任务。Ant 构建文件如下所示:

<project default="buildHello">
  <target name="compile">
    <javac srcdir="." />
  </target>

  <target name="jar" depends="compile">
    <jar destfile="hello.jar"
         basedir="."
         includes="**/*.class"
         />
  </target>

  <target name="buildHello" depends="compile,jar" />
</project>

It will be started by simply typing ant. Eclipse also has Ant support.

只需键入 即可启动ant。Eclipse 也有 Ant 支持。

> ant jar
Buildfile: build.xml

compile:

jar:
      [jar] Building jar: /Dev/hello.jar

BUILD SUCCESSFUL
Total time: 2 seconds