4 Replies Latest reply on Jul 5, 2006 12:42 AM by 796440

    Runtime exec problem

    807569
      Hello everybody,

      I need to print some lines in a printer I have attached to my parallel port, but I can't get it to work from java.

      I'm using linux, so I have my printer in: /dev/lp0

      so I'm using this command:

       r = Runtime.getRuntime();
            p = null;
            p = r.exec(  "echo hello > /dev/lp0");
      but it doesn't work.


      If I try the same command from a Linux command line it works fine. And if I try another command, like
       r = Runtime.getRuntime();
            p = null;
            p = r.exec(  "eject");
      it works perfect. What could the problem be??

      Is possible to have the same problem in Windows?

      Thanks a lot,
      Johnny
        • 1. Re: Runtime exec problem
          807569
          Does directing output to /dev/lp0 work outside of Java?

          Have you tried using lpr or whatever other command line print statements are allowed?

          Have you tried the Java print mechanism, whatever it's called?
          • 2. Re: Runtime exec problem
            807569
            Does directing output to /dev/lp0 work outside of
            Java?
            yes, the command works perfect when I called from a console, if I type:

            echo hello > /dev/lp0 works fine.

            >
            Have you tried using lpr or whatever other command
            line print statements are allowed?

            Have you tried the Java print mechanism, whatever
            it's called?
            I guess I cannot try another print mechanism, because its a receipt printer, and the drivers I have found doesn't work even on Windows :-(

            I found in the docs that Process manages its own io, this is what the javadoc for Process says:

            "
            The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess
            "
            and so now I'm trying reading the Process Input and Output streams with tihs code:
             r = Runtime.getRuntime();
                  p = null;
            
                  String line = null;
                  int tam = 0;
            
                  try {
            
                     p = r.exec( "echo hello" );
                     BufferedReader bf = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
            
                     while ( ( line = bf.readLine() ) != null ) {
                        System.out.println( line );
                        FileOutputStream fileOutputStream = new FileOutputStream(new File("/dev/lp0"));
                        fileOutputStream.write( line.getBytes());
                     }
            
                  }
                  catch ( IOException e ) {
                     e.printStackTrace();
                  }
            This code, when executed, prints hello in java console, but nothing is printed in the file on the device. First time I run it I got a:

            java.io.FileNotFoundException: /dev/lp0 (Permission denied)
                    at java.io.FileOutputStream.open(Native Method)
                    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
                    at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
                    at CommandPrintTest.main(CommandPrintTest.java:111)
            So I gave permission 777 to that file, and now the command runs ok, but I only get hello in java console and nothing in the printer.

            What could be wrong?

            and what if the machine has windows?? what should I do in that case?


            Thanks a lot,
            • 3. Re: Runtime exec problem
              796440
              Redirecting, pipes, ampersand, etc., are processed by the shell. The program you're invoking never sees them. I think echo is also a shell builtin, not an executable command. You need to either invoke a shell script that has echo whatever > blah inside it, or else exec() a shell and pass your command to it.

              The latter would be something like this, but see the man page for your shell for details--it varies among shells, and possibly among unices' implementations of those shells.
              r.exec("/bin/bash -c 'echo foo > bar'");
              // OR
              r.exec("/bin/bash", new String[] {"-c", "echo foo > bar"});
              // OR, if you're on 5.0 with varargs, possibly
              r.exec("bin/bash", "-c", "echo foo > bar"); 
              but you'll have to double check Runtime's docs for the exec flavor that lets you pull the args out into separate strings from the command.
              • 4. Re: Runtime exec problem
                796440
                Also, as general information, you'll want to read this. It may not be the source of any trouble for you here, but it's worth knowing.

                http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html