Java, Semeru Runtimes and Runtimes for Business

 View Only
Expand all | Collapse all

IBMi / org.apache.commons.exec.CommandLine

  • 1.  IBMi / org.apache.commons.exec.CommandLine

    Posted Sun December 15, 2024 03:31 AM

    Hi

    I am launching an shell script in Java IBMi  using class Java on my WAS

                   ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                   CommandLine cmd = new CommandLine("/home/myuser/test.sh");
                   PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);          
                   String[] args={ "arg1", "arg2", "arg3", "arg4", "arg5"};
                   cmd.addArguments(args);   
                    DefaultExecutor executor = new DefaultExecutor();
                   executor.setStreamHandler(streamHandler);
                   executor.setExitValue(0);
                   executor.execute(cmd);
                   System.out.println(outputStream.toString());
    The script is very simple
    #!/usr/bin/qsh

    echo File1 $1 >> test.log

    echo File2 $2 >> test.log

    java -cp >> test.log

    The problem is if I launch the shell script using a terminal 5250 throught qsh the output is OK

    but if I running from Java/WAS the output is not decode

    Output

    running
    FileIn: File1
    FileOut: File2
    ¦>#Ä#? ÀÁ áÌÁÄâ/ÈÄÇ&Ê?ÄÁËË [ î    ëÁ Ä/ÊÅ/ %/ %#ÂÊÁÊ#/ &/Ê/_ÁÈÊ?Ë  ëÈ/ÊÈ ¦à  
                        ¦>#Ä#? ÀÁ áÌÁÄâ/ÈÄÇ&Ê?ÄÄÁËË &/Ê/_ÁÈÊ?Ë ÊÁÄ#Â#À?Ë  $<ñ/Î/ 
    %/>Å ëÈÊ#>Å      //  ã#> áÌÁÄâ/ÈÄÇ&Ê?ÄÁËË $                                 

    Any idea ??

    Also I test running the command using Java class ProcessBuilder

                  Proceso = new ProcessBuilder( "/usr/bin/qsh" , "-c", "/home/myuser/test.sh" );
               Process p = Proceso.start();
               p.waitFor();

    Best regars



    ------------------------------
    Jose Luis Nebril
    ------------------------------


  • 2.  RE: IBMi / org.apache.commons.exec.CommandLine

    Posted Sun December 15, 2024 02:32 PM

    Hi José!

    • Any reason why you are using Commons Exec instead of java internal methods, like java.lang.Process?
    • java -cp will do nothing execpt printing an error...

    That aside, you never specified any encodings. The problem relies in byteArrayOutputStream.toString() -- where the .toString() is not only unnecessary, it doesn't do what you think it does.
    As the name suggests, it stores a byte array. The output you see is the way your 5250 interprets bytes -- whatever encoding that may be. It might differ from the file encoding (probably does).
    You want to read the byte array of the byteArrayOutputStream and interpret it with the correct encoding like so:

    System.out.println(outputStream.toString(StandardCharsets.UTF_8));

    If you are not on Java 10+ yet, you can use this method:

    System.out.println(new String(outputStream.toByteArray(), StandardCharsets.UTF_8));

    Replace the encoding with what's stored in your byte array OS.

    Also worth reading: -Dfile.encoding is the default if no encoding is explicitly given. Some useful links:

    • https://www.ibm.com/docs/en/i/7.4?topic=jc-default-java-fileencoding-default-charset-changed-utf-8
    • https://www.baeldung.com/java-char-encoding
    • https://www.ibm.com/docs/en/semeru-runtime-ce-z/17?topic=guide-controlling-output-encoding


    ------------------------------
    ------------------------------
    Benjamin Marwell
    System Engineer // IBM Champion // Apache Shiro PMC
    Finanz Informatik
    ------------------------------
    ------------------------------