Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to get the result from a .class, calling the process on another .java. The formatting of both files is as follows:

package Ejemplo2;

import java.io.*;

public class Ejemplo2 {

    public static void main(String[] args) throws IOException {

        Process p = new ProcessBuilder("ls", "-la").start();

        try {
            InputStream is = p.getInputStream();
            int c;
            while ((c = is.read()) != -1) {
                System.out.print((char) c);
            }
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        int exitVal;
        try {
            exitVal = p.waitFor(); //recoge la salida de System.exit()
            System.out.println("Valor de Salida: " +exitVal);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

and

package Ejemplo3;

import java.io.*;

public class Ejemplo3 {

    public static void main(String[] args) throws IOException{

        File directorio = new File("./out/production/psp-2122/Ejemplo2");

        ProcessBuilder pb = new ProcessBuilder("java", "Ejemplo2");

        pb.directory(directorio);

        System.out.printf("Directorio de trabajo: %s%n",pb.directory());

        Process p = pb.start();

        try {
            InputStream  is = p.getInputStream();

            for (int i = 0; i<is.available(); i++) {
                System.out.println("" + is.read());
            }
            
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The result only displays the directory and the exit code, but I don't really have a clue why the process itself is not shown.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
202 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should probably do:

// start in classes root
File directorio = new File("./out/production/psp-2122"); 
// Run java with fully qualified class name Ejemplo2.Ejemplo2
ProcessBuilder pb = new ProcessBuilder("java", "Ejemplo2.Ejemplo2"); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...