Run Java Program From Command Line With Arguments

  четверг 19 марта
      48

How to run Java program in command prompt. Ask Question Asked 7 years, 3 months ago. In Eclipse it runs fine. I need to run the Java program in command prompt by passing some arguments. Browse other questions tagged java compilation command-line-arguments command-prompt or ask your own question. Jan 20, 2019  Command-line arguments can be a way of specifying configuration properties for an application, and Java is no different. Instead of clicking on an application icon from the operating system, you can run the Java application from a terminal window. Along with the application name, a number of arguments can follow which are then passed to the application's starting point (i.e., the main method.

Details
Written by Nam Ha Minh
Last Updated on 04 August 2019 Print Email
In this Java tools tutorial, I will guide you how to use the java command provided in JDK (Java Development Kit) to execute Java programs.Table of content:You know, java is the Java application launcher tool which is used to execute programs written in Java programming language and compiled into bytecode class files. Its executable file can be found under JDK_HOMEbin directory (java.exe on Windows and java on Linux), so make sure you include this path to the PATH environment variable in order to invoke the program anywhere in command line prompt.

java command syntax:

java [options] file.class [arguments..]

java [options] -jar file.jar [arguments.. ]

The first syntax is for executing a class file, and the second one is for executing a JAR file.Type java -helpRun Java Program From Command Line With Arguments to consult the available options or browse Oracle’s Java documentation for detailed description and explanation of the options. The arguments, if specified, will be passed into the running program.NOTES:
    • A Java class must have the public static void main(String[] args) method in order to be executed by the Java launcher.
    • An executable JAR file must specify the startup class in by the Main-Class header in its manifest file.
Following are common usage examples of the java tool in daily Java development.

1. Run a Java program from a .class file

  • Run a simple class:

If you have a source file called MyProgram.java and it is compiled into MyProgram.class file, type the following command:

  • Run a class which is declared in a package:

If the class MyProgram.java is declared in the package net.codejava, change the working directory so that it is parent of the netcodejava directory, then type:

  • Run a class which has dependencies on jar files:
    If we have a JavaMail-based program that depends on mail.jar library. Assuming the jar file is at the same directory as the class file, type:NOTES: There must be a dot (.) after the semicolon.If the jar file is inside a directory called lib:If the program depends on more than one jar files:We can use wildcard character to refer to all jar files:Or:
  • Passing arguments to the program:

The following example passes two arguments “code” and “java” into the MyProgram:

If the argument has spaces, we must enclose it in double quotes, for example:

That will pass two arguments “code java” and “2013”.

2. Run a Java program from an executable JAR file

  • Run a standalone jar file:

Here the MyApp.jar file must define the main class in the header Main-Class of its manifest file MANIFEST.MF. The header is usually created by the jar tool.

NOTES: if the jar file depends on other jar files, the reference jar files must be specified in the header Class-Path of the jar’s manifest file. The -cp option will be ignored when using -jar flag.

  • Passing arguments:

Pass two arguments “code” and “java” to the program:

If the argument contains space, enclose it in double quotes like this:

3. Specify splash screen

For Swing-based application, we can use the -splash:imagePath flag to show a splash screen at program’s startup. For example:

Here the image SplashScreen.png is loaded as splash screen at startup.

4. Set system properties

We can use the -Dproperty=value option to specify a system property when running a program:

  • Specify a single property:

if the property’s value contains spaces, enclose it in double quotes:

  • Specify multiple properties:
  • Override predefined property:

We can override the predefined system properties. For example, the following command overrides the system property java.io.tmpdir:

5. Specify memory size

When launching a Java program, we can specify initial size and maximum size of the heap memory:
    • -Xms<size>: specifies initial heap size
    • -Xmx<size>: specifies maximum heap size.
The size is measured in bytes. It must be multiple of 1024 and is greater than 1MB for initial size and 2MB for maximum size. Append k or K to indicate kilobytes; m or M to indicate megabytes. For example, the following command launches a program with initial heap size 32MB and maximum heap size 1024MB:Other Java Tools Tutorials:

About the Author:

Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook.

From your Java program, it is possible to run other applications in your operating system. This enables you to make use of system programs and command line utilities from your Java program. Here are some typical uses of this feature when running your Java program in Windows,

  • You want to invoke a Windows program such as Notepad
  • You want to invoke a command line utility such as Check disk (chkdsk) or IP information (ipconfig)
  • You want to access the Windows command interpreter and access some of its features such as 'dir' command or 'find' command. In this case, please note that the program you want to access is cmd.exe and commands like 'dir' and 'find' are arguments to cmd.exe. Also when directly invoked, cmd.exe doesn’t terminate. If you want cmd.exe to terminate immediately, you should pass the /C argument.

We have provided sample programs for each of the use cases above. The overall approach here is same (we will use Runtime.exec method), but there are some subtle differences you need to be aware of.

The following program demonstrates how you can execute a Windows program such as Notepad from your Java program,

This following example demonstrates how you can execute a command line program in Windows from your Java program. This program will also print the results of the command. Note that in this case we are using another version of the Runtime.exec method. This program runs 'ipconfig' command with /all command line argument. This would print the complete IP address configuration of the system.

In the final example, we invoke the command line program (cmd.exe) of the Windows system and then execute commands supported by it. By default cmd.exe is blocking and to terminate it immediately you need to pass the command line option /C. The following program prints the directory listing of C drive (C:) using the 'dir' command of 'cmd.exe'.

Program langenscheidt verbtabellen deutsch pdf

If you are not reading the response from the external program and if you want to wait for the external program to finish before you continue processing, you can use the Process.waitFor() method. This will block your program till the external program completes execution.