Run java program from another

1

I have this program that I must run from another.

public class Exercici8_p1 {
   public static void main(String[] args) {
    System.out.println("Introduzca texto -> ");
    Scanner sc = new Scanner(System.in);
    String texto = sc.nextLine();
    String[] parts = texto.split("\*");
    System.out.println(parts[0]);
  }
}

From this I must execute the previous one

public class Exercici8_p1b {
 public static void main(String[] args) {
    try
    {
        Process process =  Runtime.getRuntime().exec("java Exercici8_p1");
    }
        catch(Exception e)
    {
        System.err.println("Error on exec() method");
    }
 }
}
    
asked by Caldeiro 04.11.2018 в 16:45
source

2 answers

0

I do not understand what the purpose of your goal is but you can do two things: 1. Create that as a class:

You can create your Exercici8_p1 as a class and create a normal method with what you have in the main, from the other program you can create an object of type Exercici8_p1 and call the method.

  • Use threads:
  • Remember that the type of programming that is being used is imperative programming where there is a flow of instructions, therefore calling another program is enough to run one more method, but if you want to create another process, just launch one more thread.

    link

        
    answered by 04.11.2018 в 20:24
    0

    The problem with your code and why you do not know if it works or not is because you are not printing anything from the process you are running, for this you can make these changes:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Exercici8_p1b {
    
        public static void main(String[] args) {
            try {
                Process proc = Runtime.getRuntime().exec("dir");
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                // leer la salida de correr el proceso
                System.out.println("Salida del comando:\n");
                String s;
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }
    
    
                BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
    
                // leer errores ak ejecutar el comando
                System.out.println("Salida de errores del proceso (si los hay):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
    
            } catch (IOException e) {
                System.err.println("Error al ejecutar");
            }
        }
    }
    

    Now the second, the use of Runtime.getRuntime().exec() is somewhat obsolete, to correctly process the input and output both input and errors from the process should use threads to work at the same time, so it developed a specific class to hide the required processing, class ProcessBuilder .

    Here is an example that will allow you to run your first example class:

    import java.io.IOException;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class Exercici8_p1c1 {
    
        public static void main(String[] args) {
            try {
                Path currentRelativePath = Paths.get("");
                String ss = currentRelativePath.toAbsolutePath().toString();
                System.out.println("La ruta desde donde ejecutas tu comando es: " + ss);
    
                ProcessBuilder builder = new ProcessBuilder("java", "Exercici8_p1");
                builder.redirectErrorStream(true);
                builder.inheritIO();
                Process process = builder.start();
                int errCode = process.waitFor();
                System.out.println("Error al ejecutar el comando? " + (errCode == 0 ? "No" : "Sí"));
    
            } catch (IOException e) {
                System.err.println("Error de lectoescritura");
            } catch (InterruptedException ex) {
                System.err.println("Problema con los hilos");
            }
        }
    }
    

    The InheritIO method allows you to associate the input and output flows to the current flows of your program, so you can write and read the program interactively as if you had run it directly.

        
    answered by 13.01.2019 в 04:08