• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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");}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic