Hey guys,
I have a program that I want to launch and pass a parameter. For example:

"C:\Program Files\aProgram\test.exe -login joeSmith password1"

The thing is I can't just add that to a shortcut because lots of other people will be using this computer as well. Is there a way of executing a program with parameters through java?

Thanks,
PO.

Recommended Answers

All 5 Replies

Take a look at the Runtime class. Although I have not tried it with parameters, I think all you have to do is give the entire command to execute to its exec() method and it will return you a reference to that Process.

I assume here you want to run the above command from java.
Java provides you with facilities to run commands at shell/prompt with the Runtime.exec() function
It should be something like this :

String cmd = "C:\Program Files\aProgram\test.exe -login joeSmith password1";
Runtime run = Runtime.getRuntime();
Process proc = run.exec(cmd);

Also see this (E.g. From Java Developer's Almanac)

alright thanks guys I will try this asap!

That worked great verruck and stephen!

Thanks for all your help!

By the way, you should be very wary of just making your program call Runtime.exec() and "sitting back and relaxing": if the other process logs anything out (to either the standard output or error stream), you must read the contents of those streams else things will generally freeze.

If you use a ProcessBuilder, you can call its redirectErrorStream() method so that you only have to read from one stream instead of two.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.