Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am assuming that the reason for calling Runtime.getRuntime().exec(f.getPath()); is to open up the abc.txt file in a text editor. It would be better to provide complete command for opening the text editor along with the file path. I tried this with notepad.exe (windows) and it worked.</p> <pre><code>import java.io.*; public class Demo{ public static void main(String[] args){ File f = new File("abc.txt") ; try{ System.setOut(new PrintStream( new FileOutputStream(f) ) ) ; } catch(FileNotFoundException fnfe){ System.out.println(fnfe.getMessage()) ; } System.out.println("Hello\n") ; try{ Process p = Runtime.getRuntime().exec("notepad.exe " + f.getPath()) ; } catch(IOException io){ System.out.println(io.getMessage()) ; } } } </code></pre> <p>Following code dynamically generates java code and uses javac to compile it</p> <pre><code>import java.io.*; public class Demo{ public static void main(String[] args){ File f = new File("Abc.java") ; PrintWriter writer = null; BufferedReader reader = null; InputStream pStream = null; try{ // Open File Stream and write code writer = new PrintWriter( new FileOutputStream(f) ); String javaCode = "public class Abc { \r\n" + "public static void main(String[] args) {\r\n" + "System.out.println(\"Hello World!\");\r\n" + "}\r\n" + "}\r\n"; writer.println(javaCode) ; writer.close(); // Run Javac to compile the code Process p = Runtime.getRuntime().exec("javac " + f.getPath()) ; p.waitFor(); // status = 0 =&gt; Process executed without errors // = 1 =&gt; Process executed with errors int status = p.exitValue(); if( status == 0 ) { pStream = p.getInputStream(); } else { pStream = p.getErrorStream(); } // Display the output from the process reader = new BufferedReader(new InputStreamReader(pStream)); String ln = null; while( (ln = reader.readLine()) != null ) { System.out.println(ln); } } catch(Exception ex){ System.out.println(ex.getMessage()) ; } finally{ try{ if( writer != null ){writer.close();} if( pStream != null ){pStream.close();} if( reader != null ){reader.close();} } catch(Exception ex){ System.out.println(ex.getMessage()) ; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload