please tell me how I can execute c++ program frm java

I had tried this one but it does`t work


/*import java.io.*;
public class exe1
{
public static void main(String[] arg)
{
try
{
Runtime rt = Runtime.getRuntime();
//int s=rt.availableProcessors();
Process prcs =rt.exec("inventry.exe");
InputStreamReader isr =new InputStreamReader(prcs.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line;
while((line=br.readLine())!=null)
System.out.println(line);


}
catch(IOException ioe)
{
System.exit(0);
}

}
} */

thanks sahil

please tell me how I can execute c++ program frm java

I had tried this one but it does`t work


/*import java.io.*;
public class exe1
{
public static void main(String[] arg)
{
try
{
Runtime rt = Runtime.getRuntime();
//int s=rt.availableProcessors();
Process prcs =rt.exec("inventry.exe");
InputStreamReader isr =new InputStreamReader(prcs.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line;
while((line=br.readLine())!=null)
System.out.println(line);


}
catch(IOException ioe)
{
System.exit(0);
}

}
} */

thanks sahil

hi buddy


you add this line also and try i am not sure

Runtime rt = Runtime.getRuntime();
Process prcs =rt.exec("inventry.exe");

Thread.sleep(3000);

you check wether the ex is in same folder or not


actualy your code is correct try this...
regards
rahulmenonr

Thread.sleep(3000);

Using waitFor() would be a better idea..

prcs.waitFor();

@kaushalsahil09 - Some more description of error than "I had tried this one but it does`t work" would help.

Hi,

Plz. try it.

class test {
public static void main(String a[]) {
try {
Runtime rt=Runtime.getRuntime();
Process pr=rt.exec("inventry.exe");
}catch(Exception e) {
System.out.println("Error in main() : " +e);
}
}
}

NOTE: This inventry.exe should be in the same folder that of test.class's directory, otherwise above code won't work.

hi buddy


you add this line also and try i am not sure

Runtime rt = Runtime.getRuntime();
Process prcs =rt.exec("inventry.exe");

Thread.sleep(3000);

you check wether the ex is in same folder or not


actualy your code is correct try this...
regards
rahulmenonr

Thanks Dear for your help Its really very nice of u.

Actuallly my problem is not ye solved. It gives an error . Error while setting up the enviromental variables. If u want any kind of help let me know If I am able to help u I can.

Thanks
Sahil kaushal

>> The import statement at the top does not need to be commented. (Take out the /* and *)
If some import is missing/wrong it'll give a compilation error not runtime..

This article has been dead for over six months. Start a new discussion instead.