Why does the command line utility freeze when called through a java program?

I need your suggestions and recommendations in the next task. I use libdmtx, which comes with a command line utility that reads image files for ECC200 data matrix barcodes, reads their contents, and writes decoded messages to standard output. I want to use this command line utility in my linux java program. I am using ubuntu linux. I installed libdmtx on my Linux machine. and when i call the command

dmtxread -n / home / admin / ab.tif

on linux terminal it immediately gives the decoded barcode value.

when I am going to call this command with the help of my java program, the code scripts when executing the command and dotn produce the result. it looks like the program is processing or freezing.

Below is the java code that calls the following command

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;


public class Classtest {

    public static void getCodes(){

        try 
        { 
            Process p; 
            String command[]=new String[3];
            command[0]="dmtxread";
            command[1]="-n";
            command[2]="/home/admin/ab.tif";

            System.out.println("Command : "+command[0]+command[1]+command[2]);
            p=Runtime.getRuntime().exec(command);  //I think hangs over here.

            BufferedReader reader=new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String line=reader.readLine();
            if(line==null){
                reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
                line=reader.readLine();
                System.out.print("Decoded      :- "+line);
            }else{
                System.out.print("Error      :- "+line);
            }
            System.out.println(p.waitFor());

        }catch(IOException e1) {
            e1.getMessage();
            e1.printStackTrace();
        }catch(InterruptedException e2) {
            e2.getMessage();
            e2.printStackTrace();
        } 


    }
    public static void main(String args[]){
        getCodes();
    }

}

Please tell friends where my code goes wrong.

I cite the following article but don't get any help

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1

I beg you, friends! Thank!

Here is the new code in which I used the ProcessBuilder class, this code also produced the same result as above, the code that it hangs on the line Process process = pb.start ();

public class Test {

public static void main(final String[] args) throws IOException, InterruptedException {
    //Build command 
    List<String> commands = new ArrayList<String>();
    commands.add("dmtxread");
    commands.add("-n");
    commands.add("/home/admin/ab.tif");
    System.out.println(commands);

    //Run macro on target
    ProcessBuilder pb = new ProcessBuilder(commands);
    pb.redirectErrorStream(true);
    Process process = pb.start();

    //Read output
    StringBuilder out = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = null, previous = null;
    while ((line = br.readLine()) != null){
        System.out.println(line);
    }

    //Check result
    if (process.waitFor() == 0)
        System.out.println("Success!");
    System.exit(0);

    //Abnormal termination: Log command parameters and output and throw        ExecutionException
    System.err.println(commands);
    System.err.println(out.toString());
    System.exit(1);
}

}

Please help me solve this problem. Thanks you!

+3
source share
3 answers

ReadLine , . , , readLine.

ProcessBuilder Runtime.exec(), InputStreams :

ProcessBuilder builder = new ProcessBuilder(cmd,arg0,arg1);
builder.redirectErrorStream(true);
Process process = builder.start();

, .

InputStreams.

,

+4

. stderr, , stdout.

  • stderr, 2.
  • stderr, , , Java waitFor.
  • .

, . , , .

+2

, ( , ), :

Imagine that the program wants to print 2 lines of text. Or just one line, but in stderr. Your code reads only 1 line from stdout and expects the process to complete. This means that the child program can wait for the reader to read the next line, so it waits in writeuntil someone blocks this channel - forever.

When you start dmtxreadfrom the command line, there is no lock on the output channel, so the program works exactly.

0
source

All Articles