繁体   English   中英

Java未运行shell脚本

[英]Java not running shell script

我有一个Java代码,它需要一个数据文件,将数据复制到perl脚本,然后从shell脚本调用perl脚本。 以下是相关代码:

    public String returnStringForPerlScript(ArrayList<String> arrayContainingPerlScriptArguments, String singularFilePath) throws IOException{
    String argFileName = null;//5
    String argParsedFileName = null;//
    argParsedFileName = arrayContainingPerlScriptArguments.get(4);
    argFileName = arrayContainingPerlScriptArguments.get(5);
    System.out.print("ARG1: "+argFileName);
    System.out.print(" ARG2: "+argParsedFileName);

    String runCmdString = singularFilePath+"perlscript.pl";
    String runCmd  = singularFilePath+"runPerlScript.sh";


    return runCmd;
}

上面的代码是一个单独类中的方法。 下面的代码是Process实际初始化和运行的地方。

p = dfp.returnStringForPerlScript(perlParam, singularDirectoryPath);
System.out.println("Running from: "+p);
process = Runtime.getRuntime().exec(p);

令人困惑的是,这段代码在早期就可以很好地工作,而我所更改的只是输出目录。 我写了一个完全不同的文件路径,而不是“ singularDirectoryPath”。 我很困惑,因为我不知道代码是怎么回事,考虑到它曾经运行过。 当我从终端调用“ runPerlScript.sh”时,它起作用了。

我还应该补充一点,我确实尝试使用String []而不是字符串,如下所示:

    String[] cmdArray = {"/bin/tcsh","-c","/filepath/runPerlScript.sh"};
    Process p = Runtime.getRuntime().exec(cmdArray);

仍然没有生成任何输出文件。

验证两件事:

  • 如果您修改了perl脚本,请确保它的第一行具有#!/bin/perl ,与您的shell脚本相同,如果您专门使用,则必须具有#!/bin/sh#!/bin/bash庆典。

  • 您移动的文件必须具有可执行权限,请使用进行更改。 chmod 755 myFile

您陈述问题的方式似乎无法解决。 由于这显然曾经起作用,所以发生了以下两件事之一:Java执行环境或OS环境(这就是为什么首先要考虑文件权限的原因)。

由于您可以手动执行脚本,因此最可能的原因似乎是java由于某些原因无法执行脚本。

请将以下代码添加到您的Java程序中,并检查操作系统返回的退出代码是什么。

System.out.println("Waiting for process to finish...");
process.waitFor(); // wait until the process finishes
int exitCode = process.exitValue();
System.out.println("Process exit code: " + exitCode); 

编辑:在注释中提供的说明之后,最可能的情况是Java错误地传递了脚本的命令行参数。 测试起来相对容易:只需在shell脚本命令的第一行添加echo $1 $2 $3获取尽可能多的参数。 这样,您将确定参数是否正确传递。 如果不是,那么问题就出在Java方面,我们应该使用exec()的String []版本。 我已经习惯了Processbuilder所以在示例中会用到它(请在执行上面的测试后使用下面的代码,以便我们知道缺少哪些参数):

String shellScript = "/some/path/runPerlScript.sh";
String perlScript = "/some/path/perlscript.pl";

// similar to using exec() version with String[]
ProcessBuilder pb = new ProcessBuilder(shellScript , perlScript); 

// print what the process will see as current directory
File workDir = pb.directory();
System.out.println("Working directoy:" + workDir.getAbsolutePath());

// print the environment variables visible to the process
Map<String, String> env = pb.environment(); 
System.out.println("Environment available to shell script:\n" + env);

// set the working directory if needed depending on the output of the workDir above
pb.directory(new File("/some/path")); 
Process p = pb.start();

// add the lines from the previous snipet here that waits until the process is done

我看了一些Javadocs,并针对我的问题提出了这个解决方案。 不知道确切为什么会起作用,但是会起作用!

    ProcessBuilder builder = new ProcessBuilder(p1);

        builder.directory(dirFile);
        Process process = builder.start();
        System.out.println("Process working directory: "+builder.directory().getAbsolutePath());
        InputStream is = process.getInputStream();
        InputStreamReader reader = new InputStreamReader(is);
        Scanner scan = new Scanner(reader);


        while(scan.hasNextLine()){
            System.out.println(scan.nextLine());
        }

基本上,我只是指定了ProcessBuilder的工作目录。 我也将String []而不是字符串传递给ProcessBuilder,并在字符串数组中指定了我的shell。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM