• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

how to r.exec("java filename");

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to run a .class file from Process.exec();.
Running "java filename" command is not working i.e. the .class file dont get executed.Although if i give "javac filename.java" in r.exec(); then the code get executed and the file is compiled
properly.
ANY ANSWERS..
THE CODE FOLLOWS
public class RunTimeTry1
{
public static void main(String [] qq)
{
System.out.println("in the main");
try{
Runtime r;
r = Runtime.getRuntime();
r.exec("java filename");
}catch(Exception e){System.out.println("caught");}
System.out.println("out of the main");}
}
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran a test on this as follows.
import java.io.*;
public class run{
public static void main(String args[])
{
try
{
String writeLine = "Testing";
System.out.println(writeLine);
File writeFile =
new File("c:\\java","test.txt");
DataOutputStream dataFile =
new DataOutputStream(new BufferedOutputStream(new
FileOutputStream(writeFile)));
dataFile.write((writeLine.getBytes()));
dataFile.close();
}
catch(IOException ioe)
{System.out.println("Exception : " + ioe);}
}
}
public class runtest
{
public static void main(String [] qq)
{
System.out.println("in the main");
try{
Runtime r;
r = Runtime.getRuntime();
r.exec("java run");
}catch(Exception e){System.out.println("caught");}
System.out.println("out of the main");}
}
And while the System.out.println doesn't display to the console, the file test.txt is created. Therefore I assume that the program did run but, the output is directed to a stream that you can't see. If you need the output from filename to be displayed, you could save it in a file and read it in the original program.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did a little more research and found that you can get the output from the Program run by the exec method using the getInputStream method of java.lang.Process. The new runtest.java is as follows
import java.io.*;
public class runtest
{
public static void main(String [] qq)
{
System.out.println("in the main");
try{
Runtime r;
r = Runtime.getRuntime();
Process p = r.exec("java run");
BufferedReader reader = new BufferedReader(new InputStreamReader(new DataInputStream( new
BufferedInputStream(p.getInputStream()))));
String lineRead =null;
while( (lineRead = reader.readLine() ) != null) {
System.out.println("Input :"+ lineRead+ "\n");
}

}catch(Exception e){System.out.println("caught");}
System.out.println("out of the main");}
}
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic