exec(cmdArray) to bash shell does not work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Bowling

    exec(cmdArray) to bash shell does not work

    I have a java (2.0) program with the following lines:

    String cmdArray1[] = {"lp", "-d", "hp4m", "MyFile"};
    System.out.prin tln(Runtime.get Runtime().exec( cmdArray1));

    It compliles properly, but does not print the file to the printer. It
    displays the following as the return from Runtime...:

    java.lang.UNIXP rocess@1034bb5

    It apparently is sending something out to bash and getting that result back.

    This in within SuSE 8.2 Linux running bash.
    The following line typed into the shell works properly:
    lp -d hp4m MyFile

    Anyone have any ideas?
    Thanks, John


  • Matt Smith

    #2
    Re: exec(cmdArray) to bash shell does not work

    First -- try using the full path for lp, i.e. "/usr/bin/lp" or similar
    ("which lp" should give you the full path). Interactive logins will set
    a path potentially different from the path your "Runtime" call receives,
    so Runtime may not have the path necessary to call just "lp".
    -Matt

    John Bowling wrote:[color=blue]
    > I have a java (2.0) program with the following lines:
    >
    > String cmdArray1[] = {"lp", "-d", "hp4m", "MyFile"};
    > System.out.prin tln(Runtime.get Runtime().exec( cmdArray1));
    >
    > It compliles properly, but does not print the file to the printer. It
    > displays the following as the return from Runtime...:
    >
    > java.lang.UNIXP rocess@1034bb5
    >
    > It apparently is sending something out to bash and getting that result back.
    >
    > This in within SuSE 8.2 Linux running bash.
    > The following line typed into the shell works properly:
    > lp -d hp4m MyFile
    >
    > Anyone have any ideas?
    > Thanks, John
    >
    >[/color]

    Comment

    • John Bowling

      #3
      Re: exec(cmdArray) to bash shell does not work

      That solved the problem of printing the file.
      If I specify the filename as "MyFile*", which could be handy when you have
      multiple files that are similar but with a count, it will not find the file.
      Currently I don't need to do that, but I probably will at some point.
      Thanks, John

      "Matt Smith" <matt.smith@uco nn.edu> wrote in message
      news:3FA7F50A.6 000402@uconn.ed u...[color=blue]
      > First -- try using the full path for lp, i.e. "/usr/bin/lp" or similar
      > ("which lp" should give you the full path). Interactive logins will set
      > a path potentially different from the path your "Runtime" call receives,
      > so Runtime may not have the path necessary to call just "lp".
      > -Matt
      >
      > John Bowling wrote:[color=green]
      > > I have a java (2.0) program with the following lines:
      > >
      > > String cmdArray1[] = {"lp", "-d", "hp4m", "MyFile"};
      > > System.out.prin tln(Runtime.get Runtime().exec( cmdArray1));
      > >
      > > It compliles properly, but does not print the file to the printer. It
      > > displays the following as the return from Runtime...:
      > >
      > > java.lang.UNIXP rocess@1034bb5
      > >
      > > It apparently is sending something out to bash and getting that result[/color][/color]
      back.[color=blue][color=green]
      > >
      > > This in within SuSE 8.2 Linux running bash.
      > > The following line typed into the shell works properly:
      > > lp -d hp4m MyFile
      > >
      > > Anyone have any ideas?
      > > Thanks, John
      > >
      > >[/color]
      >[/color]


      Comment

      • Matt Smith

        #4
        Re: exec(cmdArray) to bash shell does not work

        Wildcards are a shell function. I am not overly familiar with
        "Runtime.ex ec", but my guess would be that the command is executed
        directly by the system, not issued through a shell. If you really want
        the shell features (like wildcarding), you may want to issue something
        like this (with adjusted paths as needed):

        "/bin/bash -c \"/usr/bin/lp -d hp4m MyFile*\" "

        This forces the BASH shell to execute the command, and since the shell
        is handling the command, shell functions like wildcards should work.

        Note the escaped (\") quotes -- I believe (I may be wrong) that the -c
        option for bash needs quotes around it's command string. Escape the
        inner quotes with a backslash to make sure Java does not interpret them
        as closing your string.

        Hope that helps,
        -Matt


        John Bowling wrote:[color=blue]
        > That solved the problem of printing the file.
        > If I specify the filename as "MyFile*", which could be handy when you have
        > multiple files that are similar but with a count, it will not find the file.
        > Currently I don't need to do that, but I probably will at some point.
        > Thanks, John
        >
        > "Matt Smith" <matt.smith@uco nn.edu> wrote in message
        > news:3FA7F50A.6 000402@uconn.ed u...
        >[color=green]
        >>First -- try using the full path for lp, i.e. "/usr/bin/lp" or similar
        >>("which lp" should give you the full path). Interactive logins will set
        >>a path potentially different from the path your "Runtime" call receives,
        >>so Runtime may not have the path necessary to call just "lp".
        >>-Matt
        >>
        >>John Bowling wrote:
        >>[color=darkred]
        >>>I have a java (2.0) program with the following lines:
        >>>
        >>>String cmdArray1[] = {"lp", "-d", "hp4m", "MyFile"};
        >>>System.out.p rintln(Runtime. getRuntime().ex ec(cmdArray1));
        >>>
        >>>It compliles properly, but does not print the file to the printer. It
        >>>displays the following as the return from Runtime...:
        >>>
        >>>java.lang.UN IXProcess@1034b b5
        >>>
        >>>It apparently is sending something out to bash and getting that result[/color][/color]
        >
        > back.
        >[color=green][color=darkred]
        >>>This in within SuSE 8.2 Linux running bash.
        >>>The following line typed into the shell works properly:
        >>>lp -d hp4m MyFile
        >>>
        >>>Anyone have any ideas?
        >>>Thanks, John
        >>>
        >>>[/color]
        >>[/color]
        >
        >[/color]

        Comment

        Working...