简体   繁体   中英

How to launch a Java program in CMD?

I need to run a JAR file from the command prompt. Basically I need to use the following command on startup: java -jar jar-file-name.jar , but how?

Some more details:

I need a program launcher that will launch my program in 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 ?

Open you Terminal/Command console... in the folder where you have the jar that you want to execute and then write:

java -jar jar-file-name.jar

Otherwise, as Steven C suggested, make a batch file jar-file-name.bat with the command above. 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. Double click it and enjoy your Java app.

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?

This can be easily done, simply open your command prompt and type in java -jar jarname.jar . Now you're good to go!

If you want to execute a Jar file on startup you can:

Windows Environment

First Method:

go to Start/Run and then write and write REGEDIT

Go to HKEY_LOCAL_MACHINE/SOFTWARE/MICROSOFT/Windows/Current Version/Run

Append the Path to your JAR file.

Second Method

Add your JAR file into START/ALL PROGRAMS/Autostart

Linux Environment

On Linux a quick way should be:

Create a script in /etc/init.d/ to start the jar.

Remember to add & to the command. In this way the java process will run in background.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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