java jar 中的 System.out.println

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

System.out.println in jar

javaclasscommand-linejar

提问by Dallox

I have this java program that I want to package within a jar. It compiles fine and it compiles without errors into a jar. But when I double click it, it wont start. I know most jar applications uses JFrame and that works fine. But is it possible to make it command prompt/shell based? (Like Displaying System.out.println)

我有这个 java 程序,我想将它打包在一个 jar 中。它编译得很好,并且编译成 jar 没有错误。但是当我双击它时,它不会启动。我知道大多数 jar 应用程序使用 JFrame 并且工作正常。但是有可能使它基于命令提示符/shell吗?(比如显示 System.out.println)

Example is it possible to execute this code by double clicking a jar:

示例是否可以通过双击 jar 来执行此代码:

public class Hello
{

  public static void main( String[] args )
  {
    System.out.println( "Hello, World!" );
  }
}

回答by Carlos Tasada

There is no problem doing like that. But where do you expect to see the output?

这样做没有问题。但是您希望在哪里看到输出?

If you execute from the console as java -jar my jar.jaryou will see your "Hello, World".

如果您从控制台执行,java -jar my jar.jar您将看到“Hello, World”。

If you want to double-click you'll need to create a JFrame.

如果要双击,则需要创建一个 JFrame。

回答by Jeffrey

You can change the file association for jar files to have them open a console. (This is for Windows)

您可以更改 jar 文件的文件关联以让它们打开控制台。(这是针对 Windows 的)

  • Open Command Processor
  • Type ftype jarfile
  • You will get something like:
  • 打开命令处理器
  • 类型 ftype jarfile
  • 你会得到类似的东西:

jarfile="C:\Path\To\Java\bin\javaw.exe" -jar "%1 %*"

jarfile="C:\Path\To\Java\bin\javaw.exe" -jar "%1 %*"

  • Enter ftype jarfile "C:\Path\To\Java\bin\java.exe" -jar "%1" %*(You may need administrator privileges to do this).
  • Enter ftype jarfile "C:\Path\To\Java\bin\java.exe" -jar "%1" %*(您可能需要管理员权限才能执行此操作)。

回答by Marko Topolnik

I don't see anyone mentioning the obvious solution: make a .cmd (or .bat) file containing the command everyone is talking about -- java -jar YourJar.jar. You can double-click on the .cmd file and a console window will open. It will also close imediately as your program exits, so the program should wait for a keypress before exiting.

我没有看到有人提到明显的解决方案:制作一个 .cmd(或 .bat)文件,其中包含每个人都在谈论的命令 -- java -jar YourJar.jar。您可以双击 .cmd 文件,将打开一个控制台窗口。它也会在您的程序退出时立即关闭,因此程序应该在退出之前等待按键。

回答by Marko Topolnik

The output gets forwarded to the console but without eclipse, there isn't any! The only way to work around this is to launch the program in command prompt and then you will get the output from the program.

输出被转发到控制台,但没有 Eclipse,就没有!解决此问题的唯一方法是在命令提示符下启动该程序,然后您将获得该程序的输出。

回答by Raam

You can make a jar an executable by including a manifest file which contains the name of the class that has your main method (in your example that would be Hello). Hereis a link that details what you need to do.

您可以通过包含一个清单文件来使 jar 成为可执行文件,该清单文件包含具有您的主要方法的类的名称(在您的示例中为 Hello)。是一个详细说明您需要做什么的链接。

回答by AlexR

Sure. First try to run your program as following:

当然。首先尝试运行您的程序,如下所示:

java -cp yourjar.jar Main

java -cp yourjar.jar Main

where Main is a fully qualified class name of your main class.

其中 Main 是主类的完全限定类名。

When this is working fine and if you creted exacutablejar try to run your program as following:

当这工作正常并且您创建了可执行的jar 时,请尝试按如下方式运行您的程序:

java -jar yourjar.jar

java -jar yourjar.jar

When this is working double click should work too unless you mapped extension jarto program other than javawthat is done by default when you are installing JRE.

当这工作时,双击也应该工作,除非您在安装 JRE 时默认情况下将扩展映射jar到程序以外的程序javaw

回答by imanis_tn

If you want to display the "Hello, world!" String you have to execute your code from the command line:

如果要显示“Hello, world!” 您必须从命令行执行代码的字符串:

   jar -jar your_jar_name.jar