3

I made a new process, but it never finishes. I was trying with ProcessBuilder and Runtime but none of it worked, both got stuck.

Builder code:

ProcessBuilder a = new ProcessBuilder(
    "java",
    "-classpath",
    "D:\\TAP",
    "AnalizadorLexico",
    "<",
    "D:\\TAP\\Lol1.txt");
Process process=a.start();

Runtime code:

Process process=cmd.exec(
    "java -classpath D:\\TAP AnalizadorLexico < D:\\TAP\\Lol1.txt ");

The command works in Windows CMD.

Greg
  • 9,068
  • 6
  • 49
  • 91
  • What results are you getting for both? Is there an exception being thrown? The process "never finishes" but does it at least produce the result you expect, .i.e does it run the Java class you specified? – Val May 16 '15 at 03:28
  • 1
    The "<" works with cmd(or other shells). Java program does not interpret it as input. You can use "cmd /c java progr < input ... A better way will be to use real Java APIs for it: https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html and https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html – Jayan May 16 '15 at 03:30
  • @Jayan The OP said the process got stuck which shouldn't be the case looking at the code. – geekprogrammer May 16 '15 at 03:46
  • I am aware of other cases( discussed in http://stackoverflow.com/questions/3643939/java-process-with-input-output-stream) Unless OP fixes or provide answers to above comments, cannot help much. – Jayan May 16 '15 at 03:58
  • 1
    @Jayan tanks a LOT!!!! that works =) really tankyou. – Nathan Kross May 16 '15 at 04:00
  • Which way? Did you use cmd /c or java API. You _should_ go with Java API :) – Jayan May 16 '15 at 04:10
  • @Jayan i Used cmd /c =) – Nathan Kross May 16 '15 at 04:32
  • @Jayan - it sounds like your comment should be an answer. – Richard Neish May 18 '15 at 10:10
  • The question is going to be deleted as it is duplicate or too broad. Adding as answer. Just in case. – Jayan May 18 '15 at 10:39

1 Answers1

0

From comments:

The "<" works with cmd(or other shells). Java program does not interpret it as input. You can use "cmd /c java progr < input ", but that makes it windows specific.

A better way will be to use real Java APIs for it: See ProcessBuilder

Once you get past this , please check another FAQ item on this

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143