2

I'm trying to run a Perl script file from java code but it's not working with me. I modified the Perl script and put the arguments in it instead of passing them via java code. The script works fine when running it from the command line but it's not working inside java code, always prints "wrong"!!. I wrote another Perl script (test.pl) and it's working but the desired script doesn't?? I'm working in netbeans7.3.1 (ubuntu). Here is my code:

package program;

import java.io.*;
//import java.lang.ProcessBuilder;
/**
 *
 * @author seed
 */
public class Program {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException,Exception {
        File input = new File("//home//seed//Downloads//MADA-3.2//sample");
        FileOutputStream out = new FileOutputStream(input);
        PrintWriter p = new PrintWriter(out);
        String s = "قصدنا في هذا القول ذكر";
        p.println(s);

        p.close();

        Process pro = Runtime.getRuntime().exec("perl /home/seed/Downloads/MADA+TOKAN.pl");

       pro.waitFor();
         if(pro.exitValue() == 0)  
                {  
                    System.out.println("Command Successful");  

                }  
         else{
         System.out.print("wrong");}   

        // TODO code application logic here
    }
}
Rawan
  • 21
  • 2
  • You sure that it's supposed to have exit value 0 if it works? – tbodt Jul 28 '13 at 09:05
  • Why is that // necessary for the path when you are using ProcessBuilder? – SSaikia_JtheRocker Jul 28 '13 at 09:12
  • You can probably also capture the inputstream buffer and check what's going on. – SSaikia_JtheRocker Jul 28 '13 at 09:14
  • Please check this post: [Java not running shell script](http://stackoverflow.com/questions/17868668/java-not-running-shell-script). Use the `ProcessBuilder` approach and try to set the working directory – c.s. Jul 28 '13 at 10:45
  • I put ProcessBuilder as a comment after trying it. It's not a part of the code I'm working on. I think I will update the code to make it more clear. – Rawan Jul 28 '13 at 10:49
  • Regardless of the exit value when executing the Perl it should produce output file as a result. But when executing the command inside java no output files are produced. – Rawan Jul 28 '13 at 10:59
  • `//` should simply be `/`. Do a `println("wrong");` not `print`. – Joop Eggen Jul 28 '13 at 11:04
  • You’re printing non-ASCII to the child process. You need to arrange to encode that on the way out and decode it on the way in, probably by setting the streams of both to UTF-8. [See ***this answer*** for an example of how to do that in both Perl and Java](http://stackoverflow.com/a/9853261/471272). You will also have to tell the Java compiler the source encoding to start with; that you do via an encoding argument to `javac`. – tchrist Jul 31 '13 at 12:36

2 Answers2

0

My guess is that some kind of string/path conversion issue.

I see utf8 strings in your code, maybe the path is converted to something.

The filename (MADA+TOKAN.pl) contain special char, it would be better MADAplusTOKAN.pl.

Also your string in script and in question are not the same: (MADA 3.2 != MADA-3.2)

perl MADA+TOKAN.pl config=/home/seed/Downloads/mada/MADA-3.2/config files/template.madaconfig file=/home/seed/Downloads/mada/MADA 3.2/inputfile

vs

perl MADA+TOKAN.pl config=/home/seed/Downloads/MADA-3.2/config-files/template.madaconfig file=/home/seed/Downloads/MADA-3.2/sample

user1126070
  • 5,059
  • 1
  • 16
  • 15
  • Thank you so much for the note. I correct the string in the question. I tried to run the code after rename the Perl file to MADA.pl but still not working!! I think the special char wasn't the problem – Rawan Jul 31 '13 at 08:54
  • Well, there are conversion issues, but this isn’t the argument issue. – tchrist Jul 31 '13 at 12:37
0

It sounds like it is finding your perl script and executing it, since test.perl and MADA.perl run OK.

It does sound like the arguments being passed in to the perl script are not what was expected. Can you modify the perl script to echo all its input parameters to a file?

Graham Griffiths
  • 2,196
  • 1
  • 12
  • 15