Runtime.getRuntim().exec("enter command here);
Is there a way to have this command executed in the same process that I have running where i execute the jar file that this java application would be in?
This is a java command! But you said in the beginning, you are writing a C-program that launches the Java program (jar-file) and not the other way around!
That is contradictionary. Please clarify.
So what do you want to do now?
1.) writing a c program to launch and steer an already existing java program?
You could easily decompile the java-program (google for java decomiler) and change its source code (public static void main(String[] args)) to accept commands either from the command line or from a file or current input stream.
2.) writing a java program to launch and steer an already existing C-program?
You can use a windows macro recorder (google for macro recorder), start it, steer your C-program, stop it and then execute the generated macro with runtime.exec("macrorecorder.exe generatedMacroFile");
But first, in any case, choose the easy way: figure out if the program supports
command line parameter (case 1) or reads/writes to the standard input/output stream (case 2). Most programs do. If you have a program that does not, you only have the option mentioned above.
Most people don't know that for example winRAR or internetExplorer accepts command lines arguments. I have successfully called these programs in my own java program to automatically compress directories or pop up a webpage.
You can make a quick test by typing into the windows-command-shell: "myprogram.exe /help" or "myprogram.exe -help" and see the output.
For case 2 you can make a quick test: write your commands into a text file and type into the windows-command-shell: "myprogram.exe < mycommands.txt". If that works, then you can use following code snippet below to steer your program.
This is a java forum, so I will give you the java code here only. But the C-code would be very similar and use the same principle.
- Process p = Runtime.getRuntime().exec("myprogram.exe argument1 argument2");
-
InputStream i = p.getInputStream();
-
InputStream e = p.getErrorStream();
-
OutputStream o = p.getOutputStream();
now you can just write your commands into the output stream and listening to the output or errors of your program from the input streams.
For example to issue a command:
- ps = new PrintStream(o);
-
ps.println("myCommand");
-
ps.close();