Hi,
I am a student of CSE. I am currently beginning to program with java using netbeans. I know some C++. I am now doing a project where I have to call some other program, from within the java program. I know there is a function in C/C++ called system("program.exe"), which can call an external exe. I have used it too. Is there any way I can do something in java?
System() function in java project
You have to – declare a Runtime object and assign it the current runtime – Create a process using the runtime object.exec("the address of your exe") – wait for the process to be exited Here is a small function in java to help you get started. public int runProgram() { int ret = -1; try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("cmd.exe /c a.exe"); proc.waitFor(); ret = proc.exitValue(); } catch (Throwable t) { t.printStackTrace(); return ret; } return ret; }
System() function in java project
Thanks for the reply but the function doesn't seem to work properly. I can see it calling something, but no popups appear.
System() function in java project
Thanks Apoclypse but the tutorial is out of my limit of understanding. Is there any Simple way. I don't need to manipulate the program inside java just a call would be enough.
System() function in java project
I guess your program is a command line one. Use, Process proc = rt.exec("cmd.exe /c  start  yourprogram.exe"); instead of, Process proc = rt.exec("cmd.exe /c yourprogram.exe"); This shall start a popup cmd screen as you need.