borrowed this shamely from here

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

System.out.printf("Output of running %s is:", Arrays.toString(args));

while ((line = br.readLine()) != null) {
  System.out.println(line);
}

More information here

Other issues on how to pass commands here and here

Answer from Steven on Stack Overflow
Discussions

javafx - How Can I execute external program with arguments in Java ...
I want to launch external Program with Arguments(one of the argument is to load the Config file to that external program) after I click the button of javafx app(the btn event not included in the co... More on stackoverflow.com
🌐 stackoverflow.com
exec - Executing an external program in Java with arguments - Stack Overflow
If I execute the java program it outputs Usage: shufflet [OPTIONS] NSEQ ORDER OUTFILE which is the error message that Shufflet gives if the parameters are wrong. More on stackoverflow.com
🌐 stackoverflow.com
java - Call an executable and pass parameters - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
Need to pass Java argument at runtime on client side
Would writing a .bat file to run the jar file with the command line arguments be something that would work? More on reddit.com
🌐 r/javahelp
6
2
November 1, 2023
🌐
TutorialsPoint
tutorialspoint.com › how-to-execute-an-external-program-like-windows-media-player-in-java
How to execute an external program like windows media player in Java?
October 11, 2019 - Therefore, to run an external program using the ProcessBuilder class − · Instantiate the ProcessBuilder class by passing the command to execute a process and arguments for it as parameters to its constructor. Execute the process by invoking the start() method on the above created object.
🌐
More of Less
moreofless.co.uk › system-exec-java-not-working
How to exec external commands in Java with correct parameters
All good, now the problems start when we want to send a parameter of Mickey Mouse and to do that on the command line we'd put double-quotes around it to make sure the application treats its as one parameter, our code is now · Process process = Runtime.getRuntime().exec("helloworld \"Mickey Mouse\" param2"); int exitCode = process.waitFor(); System.out.println(exitCode);
🌐
GeeksforGeeks
geeksforgeeks.org › java › calling-external-program-java-using-process-runtime
Calling an External Program in Java using Process and Runtime - GeeksforGeeks
July 23, 2025 - Subsequently, the Runtime class provides a portal to interact with the Java runtime environment. It contains methods to execute a process, give the number of available processors, display the free memory in the JVM, among others. ... // A sample Java program (Written for Windows OS) // to demonstrate creation of external process // using Runtime and Process class CoolStuff { public static void main(String[] args) { try { // Command to create an external process String command = "C:\Program Files (x86)"+ "\Google\Chrome\Application\chrome.exe"; // Running the above command Runtime run = Runtime.getRuntime(); Process proc = run.exec(command); } catch (IOException e) { e.printStackTrace(); } } } Runtime.getRuntime() simply returns the Runtime object associated with the current Java application.
🌐
Quickprogrammingtips
quickprogrammingtips.com › java › how-to-run-external-program-in-java.html
How to Run External Program in Java
The following program demonstrates how you can execute a Windows program such as Notepad from your Java program, /** * Runs external application from this Java program */ public class RunProgram { public static void main(String[] args) { RunProgram rp = new RunProgram(); rp.openNotePad(); } /** * Runs Notepad program in the Windows system.
🌐
Coderanch
coderanch.com › t › 643618 › java › execute-command-line-open-external
How to execute command line or open an external application without return values from Java? [Solved] (Java in General forum at Coderanch)
December 12, 2014 - The code I use to start an external application that remains active after I close the Java is illustrated by this ... Class SyncPipe is a Runnable that just copies the InputStream to the OutputStream; should be very easy for you to write. Using 'start with cmd.exe' in effect decouples the firefox from the Java program and the stdout and stderr output from the Process is that from cmd.exe and not firefox.
Find elsewhere
🌐
CodingTechRoom
codingtechroom.com › question › how-to-execute-exe-from-java-parameters
How to Execute an EXE File from Java with Parameters? - CodingTechRoom
To execute an executable file (.exe) in Java and pass parameters, you can utilize the `ProcessBuilder` class.
🌐
Real's HowTo
rgagnon.com › javadetails › java-0014.html
Execute an external program - Real's Java How-to
You use the RUNAS command from the command line to start an application under another account (not available with XP Home edition). There are many switches that can enhance the behaviour of RUNAS.
🌐
Stack Overflow
stackoverflow.com › questions › 8351454 › executing-an-external-program-in-java-with-arguments
exec - Executing an external program in Java with arguments - Stack Overflow
Process p = Runtime.getRuntime().exec("./shufflet 1 2 <in.seq> out.seq"); BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream())); String line; while ((line = bri.readLine()) != null) { System.out.println(line); } bri.close(); while ((line = bre.readLine()) != null) { System.out.println(line); } bre.close(); p.waitFor(); Basically, this program that I'm executing (Shufflet) reads in whatever is in in.seq and then writes something to out.seq based on that.
🌐
DocStore
docstore.mik.ua › orelly › java › fclass › ch10_04.htm
[Chapter 10] 10.4 External Program Execution
The Runtime.exec() method allows a Java program to run an external program. There are four forms of the exec() method, but they all expect to receive command-line-style information about the program to be run. The simplest exec() method takes a single String argument that contains the name ...
🌐
Learn Java
learnjavaonline.org › en › Compiling_and_Running_with_Arguments
Compiling and Running with Arguments - Learn Java - Free Interactive Java Tutorial
To run it, we need to run java with the name of the class as the argument (Not the file!) ... The main methods get an array of strings as an argument, these are the command line arguments you may pass to your program.
🌐
GitHub
gist.github.com › 2225134
How to run an external program and get the return value · GitHub
How to run an external program and get the return value - RunExternalProgram.java
🌐
Reddit
reddit.com › r/javahelp › need to pass java argument at runtime on client side
r/javahelp on Reddit: Need to pass Java argument at runtime on client side
November 1, 2023 -

I have a application which was previously using JNLP for downloading jars I replaced this JNLP with a fatjar. Everything works fine until Java 15 but due to reflective access issue I am unable to run this fatjar in java 17 and above I found a fix for this but it involves passing an argument at runtime. Now this fatjar is downloaded at client's machine and he won't run this fatjar with cmd line he would just double click it and the fatjar would run but without those jvm argument. Is there any way to embedd this jvm argument in that fatjar such that it runs successfully on double click.

P.S. : I am new to software industry. Only 3 months in experience. Thanks.

Top answer
1 of 3
2
Would writing a .bat file to run the jar file with the command line arguments be something that would work?
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Spiceworks
community.spiceworks.com › programming & development
Run an exe from java alongwith passing parameters to it. - Programming & Development - Spiceworks Community
January 7, 2011 - Hey Want to write a code which will run an exe using java .that exe is normally run using command line and you need to pass the parameters to that exe needed for its operation. i.e I need to write on command prompt wget.exe parameter1 parameter2
Top answer
1 of 4
5

This is normal: you are attempting to launch a command normally issued by a shell.

Here, <proof.in and >proof.out are taken as literal arguments to the otter executable, and not shell redirections. But seeing the home page for this tool, it will not work: it expects data on stdin, which the redirect normally provides.

You need to launch this command via a shell, and preferably using a process builder:

final ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "otter <proof.in >proof.out");
final Process p = pb.start();

etc etc.

You should also ensure, of course, that the program runs from the correct directory -- fortunately, ProcessBuilder also allows you to do that.

2 of 4
0

Here is a link to another question with a very detailed answer on how to do this correctly in an async way with threads. This is the only way to not block your main thread and hang your GUI.

private class ProcessResultReader extends Thread
{
    final InputStream is;
    final String type;
    final StringBuilder sb;

    ProcessResultReader(@Nonnull final InputStream is, @Nonnull String type)
    {
        this.is = is;
        this.type = type;
        this.sb = new StringBuilder();
    }

    public void run()
    {
        try
        {
            final InputStreamReader isr = new InputStreamReader(is);
            final BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null)
            {
                this.sb.append(line).append("\n");
            }
        }
        catch (final IOException ioe)
        {
            System.err.println(ioe.getMessage());
            throw new RuntimeException(ioe);
        }
    }

    @Override
    public String toString()
    {
        return this.sb.toString();
    }
}

then you use the above class like so

try
{
    final Process p = Runtime.getRuntime().exec(String.format("cmd /c %s", query));
    final ProcessResultReader stderr = new ProcessResultReader(p.getErrorStream(), "STDERR");
    final ProcessResultReader stdout = new ProcessResultReader(p.getInputStream(), "STDOUT");
    stderr.start();
    stdout.start();
    final int exitValue = p.waitFor();
    if (exitValue == 0)
    {
        System.out.print(stdout.toString());
    }
    else
    {
        System.err.print(stderr.toString());
    }
}
catch (final IOException e)
{
    throw new RuntimeException(e);
}
catch (final InterruptedException e)
{
    throw new RuntimeException(e);
}