简体   繁体   English

如何在CMD中启动Java程序?

[英]How to launch a Java program in CMD?

I need to run a JAR file from the command prompt. 我需要从命令提示符下运行JAR文件。 Basically I need to use the following command on startup: java -jar jar-file-name.jar , but how? 基本上,我需要在启动时使用以下命令: java -jar jar-file-name.jar ,但是如何?

Some more details: 一些更多的细节:

I need a program launcher that will launch my program in cmd. 我需要一个程序启动器,它将在cmd中启动我的程序。 Example code: 示例代码:

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

How can I run this automatically in cmd without using java -jar jar-file-name.jar ? 如何不使用java -jar jar-file-name.jar在cmd中自动运行此命令?

Open you Terminal/Command console... in the folder where you have the jar that you want to execute and then write: 在拥有要执行的jar的文件夹中,打开终端/命令控制台...,然后编写:

java -jar jar-file-name.jar

Otherwise, as Steven C suggested, make a batch file jar-file-name.bat with the command above. 否则,按照Steven C的建议,使用上面的命令创建一个批处理文件jar-file-name.bat Remember the path of the file. 记住文件的路径。

To make a batch file, you just need to create a txt file with the command above, save it and rename it with the .bat extension. 要制作一个批处理文件,您只需要使用上面的命令创建一个txt文件,将其保存并使用.bat扩展名重命名即可。 Double click it and enjoy your Java app. 双击它,享受您的Java应用程序。

If you want to merge the batch file and the jar one, try to give a look at this How to create a Java application which can be run by a click? 如果要合并批处理文件和jar文件,请尝试看一下如何创建可通过单击运行的Java应用程序?

This can be easily done, simply open your command prompt and type in java -jar jarname.jar . 只需打开命令提示符并输入java -jar jarname.jar即可轻松完成。 Now you're good to go! 现在您可以出发了!

If you want to execute a Jar file on startup you can: 如果要在启动时执行Jar文件,则可以:

Windows Environment Windows环境

First Method: 第一种方法:

go to Start/Run and then write and write REGEDIT 转到开始/运行,然后编写并写入REGEDIT

Go to HKEY_LOCAL_MACHINE/SOFTWARE/MICROSOFT/Windows/Current Version/Run 转到HKEY_LOCAL_MACHINE / SOFTWARE / MICROSOFT / Windows /当前版本/运行

Append the Path to your JAR file. 将路径追加到您的JAR文件。

Second Method 第二种方法

Add your JAR file into START/ALL PROGRAMS/Autostart 将您的JAR文件添加到START / ALL PROGRAMS / Autostart中

Linux Environment Linux环境

On Linux a quick way should be: 在Linux上,一种快速的方法应该是:

Create a script in /etc/init.d/ to start the jar. /etc/init.d/中创建一个脚本以启动jar。

Remember to add & to the command. 请记住在命令中添加&。 In this way the java process will run in background. 这样,java进程将在后台运行。

The start script should be 开始脚本应该是

#!/bin/bash

java -jar myapp.jar &

If I didn't understand the question, I'm sorry. 如果我不明白这个问题,对不起。 When I read on startup I've interpreted in this way. 当我在启动时阅读时,我是用这种方式解释的。

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

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