Skip to main content
gamedev.net
🔒 Locked

[java] train_sarray 13 T2.stringt2.sarray on cmd able to run in java???

Started by ug02070 Jan 24, 2006 at 7:53 AM 0 replies 350+ views
Original Post
ug02070
ug02070
hi, i can run the following code form cmd: train_sarray.exe -a 13 t.sarray as i ahve created an exe of c files, but i need to run it in java, i am new to processes but i thought that all of the above were arguments, i now find that they are not. t.sarray apparently redirects T2.string to the process and the output to t.sarray, does anyone know how to run this in java? thanks ps i already have the following:
[source=]
public static void train_sarray(String[] cmdline) throws IOException, InterruptedException
{
			
   String arg = "C:\\Documents and   Settings\\Danny\\Desktop\\sarray_exe\\train_sarray.exe -a 13 <T2.string>foo.sarry";

				/*Process p=Runtime.getRuntime().exec(arg);
				p.waitFor();
				System.out.println(p.exitValue());*/
			
			
				//String arg = "./train_sarray -a 4  <T1.string>foo.sarry";

				Process p=Runtime.getRuntime().exec(arg);
				BufferedInputStream eo = new  BufferedInputStream(p.getErrorStream());
				BufferedInputStream o = new  BufferedInputStream(p.getInputStream());
				p.waitFor();
				print(eo);
				print(o);
				System.out.println(p.exitValue());
	}
		
			    private static void print(BufferedInputStream out)throws IOException{
				System.out.println("");
				int r = out.read();
				while (r!=-1){
				    System.out.print((char)r);
				    r=out.read();
				}
				System.out.println("");
			    }

Gorax
Gorax
Here's some code based on something I wrote a few years ago. Assuming nothing vital's changed, it should give you what you're looking for (well the basics anyway).

String executable = ...;String input = ...;String result = "";try{	Process pr = Runtime.getRuntime().exec(executable);	BufferedReader r = new BufferedReader(new InputStreamReader(pr.getInputStream()));	PrintStream w = new PrintStream(pr.getOutputStream());	//send input	w.print(input);	w.close();	//get output	result = r.readLine();	//append new lines while the stream is ready	while (r.ready()){		result += "\n" + r.readLine();	}	//close reader	r.close();	//wait for the program to finish	int n = pr.waitFor();}catch (Exception any){	//do whatever you need to do}


[EDIT]
It probably would have helped if I added the 'result' string...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.