1
votes

I am trying to invoke non linux command on linux using java code. The libraries required for that command are installed on my linux machine. Here is my java code which invokes the command using Runtime.getRuntime().exec();

The command reads the borcode from the image file and decodes it and shows the value on console.

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class demo {
    public static void main(String args[]){
        getcodes();
    }
    public void getCodes(){
        try 
        { 
            Process p; 
            String command[]=new String[3];
            command[0]="dmtxread ";
            command[1]="-n ";
            command[2]="/home/administrator/sandip/xyz.tif";
            System.out.println("Command : "+command[0]+command[1]+command[2]);
            p=Runtime.getRuntime().exec(command);

            System.out.println(p.waitFor());
            BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line=reader.readLine();
            System.out.print("Decoded      :- "+line);
        }
        }catch(IOException e1) {
            e1.getMessage();
            e1.printStackTrace();
        }catch(InterruptedException e2) {
            e2.getMessage();
            e2.printStackTrace();
        } 
    }
}

As when I run this java code on linux I get following exception

part of exception is as follows:

Command : dmtxread -n /home/administrator/sandip/xyz.tif java.io.IOException: Cannot run program "dmtxread ": java.io.IOException: error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:475) at java.lang.Runtime.exec(Runtime.java:610) at java.lang.Runtime.exec(Runtime.java:483) at leadertechbarcode.TwoDBarCodeReadHelper.getCodes(TwoDBarCodeReadHelper.java:53)

Some times the program hangs afterinvoking the following code line p=Runtime.getRuntime.exec(Command)

when I copy the command printed by the code and runs it on terminal it runs properly.

Please tell me friends in this problem.

Is there any other way to invoke this command using java?

Thanks You!

1

1 Answers

2
votes

The Runtime.exec(String[]) method that you are using expects the first element to be the command and the following elements to be individual arguments. As such, if there are any spaces in them they will be escaped or quoted before being passed to the underlying operating system.

In your case, command[0] contains the name of the command followed by a space. This will cause the system to search for and execute a command which has that space in its name. This can not be found.

To solve this problem you should either remove the spaces surrounding the contents of each of the elements in command, or you can concatenate them manually and pass them in as a single string to the Runtime.exec(String) method instead. Note that you also have a space trailing your "-n" argument. You will likely need to remove that one as well.