Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: How to capture or save the output given by java program.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to capture or save the output given by java program.

    I want to save the output of a java program in a file.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: How to capture or save the output given by java program.

    How is the output created and where is it written? Can you post a small, sample program that creates the output you want to write to a file?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to capture or save the output given by java program.

    class Demo
    {
    public static void main(String arg[])
    {
    System.out.println("Yes!!!");
    }
    }


    Above code is written in a file Demo.java.



    I am using following code to run that file.



    private static void printLines(String name, InputStream ins) throws Exception {
    String line = null;
    BufferedReader in = new BufferedReader(
    new InputStreamReader(ins));
    while ((line = in.readLine()) != null) {
    System.out.println(name + " " + line);
    }
    }

    private static void runProcess(String command) throws Exception {
    Process pro = Runtime.getRuntime().exec(command);
    pro.waitFor();
    printLines(command + " stdout:", pro.getInputStream());
    printLines(command + " stderr:", pro.getErrorStream());
    pro.waitFor();
    System.out.println(command + " exitValue() " + pro.exitValue());
    }

    public void run()
    {
    try {
    runProcess("javac Demo.java");
    runProcess("java Demo");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }


    When i am invoking run() it is showing an error:


    00:32:54,073 INFO [STDOUT] java Demo stderr: java.lang.NoClassDefFoundError: Demo (wrong name: demo)
    00:32:54,073 INFO [STDOUT] java Demo stderr: at java.lang.ClassLoader.defineClass1(Native Method)
    00:32:54,073 INFO [STDOUT] java Demo stderr: at java.lang.ClassLoader.defineClass(Unknown Source)
    00:32:54,073 INFO [STDOUT] java Demo stderr: at java.security.SecureClassLoader.defineClass(Unknow n Source)
    00:32:54,073 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader.defineClass(Unknown Source)
    00:32:54,073 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader.access$100(Unknown Source)
    00:32:54,073 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader$1.run(Unknown Source)
    00:32:54,073 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader$1.run(Unknown Source)
    00:32:54,073 INFO [STDOUT] java Demo stderr: at java.security.AccessController.doPrivileged(Native Method)
    00:32:54,074 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader.findClass(Unknown Source)
    00:32:54,074 INFO [STDOUT] java Demo stderr: at java.lang.ClassLoader.loadClass(Unknown Source)
    00:32:54,074 INFO [STDOUT] java Demo stderr: at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    00:32:54,074 INFO [STDOUT] java Demo stderr: at java.lang.ClassLoader.loadClass(Unknown Source)
    00:32:54,074 INFO [STDOUT] java Demo stderr: at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)
    00:32:54,074 INFO [STDOUT] java Demo stderr: Exception in thread "main"
    00:32:54,074 INFO [STDOUT] java Demo exitValue() 1

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: How to capture or save the output given by java program.

    Can you make a small, complete program that compiles, executes and shows the problem? Be sure to wrap the code in code tags when you post it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to capture or save the output given by java program.

    Is there any method in any API through which i can get the output of simple java program.
    Like there is a method Diagnostic.getDiagnostic() which returns a list of Compiler errors in a program.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: How to capture or save the output given by java program.

    What kind of output are you asking about? Some output goes to a file, some goes to a socket, some goes to the console.

    returns a list of Compiler errors in a program.
    Are you asking about any java program or about the javac compiler?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to capture or save the output given by java program.

    i want to capture the output that goes to console.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: How to capture or save the output given by java program.

    What happens when you use a Process to execute the program? Is the program's console output written to the stream?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to capture or save the output given by java program.

    i don't know exactly, i got this code from my friend even he doesn't know how it is working.
    Do you have any idea?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: How to capture or save the output given by java program.

    Can you make a small, complete program that compiles, executes and shows the problem? Be sure to wrap the code in code tags when you post it.

    java Demo stderr: java.lang.NoClassDefFoundError: Demo (wrong name: demo)
    That line says there was an error when "java Demo" commandline was executed
    The name of the class was wrong: demo vs Demo
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to capture or save the output given by java program.

    public List<Diagnostic<? extends JavaFileObject>> checkCompile()
    {

    System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_05"); //jdk path

    String fileName="E:\\ProjectExp\\Demo.java"; // file path
    File[] javaFiles = new File[]{new File(fileName)};
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    try
    {
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asL ist(javaFiles));
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null,
    null, compilationUnits);
    boolean success = task.call();
    System.out.println(success);
    if(!success)
    // System.out.println(diagnostics.getDiagnostics());

    return diagnostics.getDiagnostics();
    fileManager.close();
    }
    catch (Exception e) {
    e.printStackTrace();
    // TODO: handle exception
    }
    return diagnostics.getDiagnostics();
    }

    In the above code you just need to write the path of jdk and path of file that contains the java code.


    File Nameemo.java
    File Content:


    public class Demo
    {
    public static void main(String arg[])
    {
    System.out.println("Yes!!!");
    }
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: How to capture or save the output given by java program.

    If you want me to test the program, you need to make a small, complete program that compiles, executes and shows the problem? Be sure to wrap the code in code tags when you post it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: July 26th, 2013, 03:25 AM
  2. How to capture signature using Canvas and save in png format?
    By aelynne in forum File I/O & Other I/O Streams
    Replies: 28
    Last Post: June 3rd, 2012, 10:43 AM
  3. How to capture standard output from another program ?
    By ni4ni in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2010, 10:00 AM
  4. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 04:12 AM
  5. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM