Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自java的linuxshell命令_Java_Linux_Shell - Fatal编程技术网

来自java的linuxshell命令

来自java的linuxshell命令,java,linux,shell,Java,Linux,Shell,我正在尝试从Java运行Linux命令。该命令在命令行本身中运行得非常完美,但在Java中却没有。 命令行中的命令是 curl -H "Content-Type: text/plain" -d "$(printf '#Genes\nPIK3C2A\nPTEN\nUNC5B')" -X POST --url https://reactome.org/AnalysisService/identifiers/projection/ 来自Java的命令是 String $notifyMsg="\"$

我正在尝试从Java运行Linux命令。该命令在命令行本身中运行得非常完美,但在Java中却没有。 命令行中的命令是

curl -H "Content-Type: text/plain" -d "$(printf '#Genes\nPIK3C2A\nPTEN\nUNC5B')" -X POST --url https://reactome.org/AnalysisService/identifiers/projection/
来自Java的命令是

 String $notifyMsg="\"$(printf '#Genes\nPIK3C2A\nPTEN\nUNC5B')\"";
 String $reactome = "https://reactome.org/AnalysisService/identifiers/projection/";
 String $notifyTitle= "\"Content-Type: text/plain\"";
 String $command = "curl" + " " + "-H"+ " " +  $notifyTitle + " "+ "-d " + $notifyMsg +" "+ "-X" + " " + "POST" + " " +  " " + "--url" +" " + $reactome;
 System.out.println("Command is: " + $command);

      Process p=Runtime.getRuntime().exec($command);
      p.waitFor();
      System.out.println("Run Result " + p.exitValue() )
我被要求得到这样的输出

{"summary":{"token":"MjAxODAxMjMxNjQyMDZfNjcy","projection":true,"interactors":false,"type":"OVERREPRESENTATION","sampleName":"Genes","text":true},"expression":{"columnNames":[]},"identifiersNotFound":0,"pathwaysFound":51,"pathways":

但是我得到了0。谁能告诉我哪里不对吗?

引用和逃避是你的敌人。最好的办法是完全避免它们。使用一个方法启动该过程,该方法允许您将各个参数作为单独的字符串传递,而无需将它们分开

Process p = new ProcessBuilder(
    "curl",
    "-H", "Content-Type: text/plain",
    "-d", "#Genes\nPIK3C2A\nPTEN\nUNC5B",
    "-X", "POST",
    "--url", "https://reactome.org/AnalysisService/identifiers/projection/"
).start();
您的代码调用
exitValue()
,这将为您提供进程的退出代码。要读取其输出,请从其


引用和逃避是你的敌人。最好的办法是完全避免它们。使用一个方法启动该过程,该方法允许您将各个参数作为单独的字符串传递,而无需将它们分开

Process p = new ProcessBuilder(
    "curl",
    "-H", "Content-Type: text/plain",
    "-d", "#Genes\nPIK3C2A\nPTEN\nUNC5B",
    "-X", "POST",
    "--url", "https://reactome.org/AnalysisService/identifiers/projection/"
).start();
您的代码调用
exitValue()
,这将为您提供进程的退出代码。要读取其输出,请从其

p.exitValue()
将为您提供进程的退出代码。它返回0,因为curl成功,退出代码为0

<>我会考虑在java中用这些方法来执行这些操作。p>
p.exitValue()
将为您提供进程的退出代码。它返回0,因为curl成功,退出代码为0


<>我会考虑在java中用这些方法来执行这些操作。p> 这就是我的工作原理:

String notifyMsg = "#Genes\nPIK3C2A\nPTEN\nUNC5B";
String reactome = "https://reactome.org/AnalysisService/identifiers/projection/";
String notifyTitle = "Content-Type: text/plain";

Process p = new ProcessBuilder("curl", "-H", notifyTitle, "-d", notifyMsg, "-X", "POST", "--url", reactome).start();
p.waitFor();
try (Scanner scanner = new Scanner(new InputStreamReader(p.getInputStream()))) {
    while (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
    }
}
System.out.println("Run Result " + p.exitValue());
  • 不要手动控制命令行,请使用
    ProcessBuilder的vararg构造函数
  • 您的
    内容类型
    指令和
    -d
  • $(printf
    是shell解释的东西,但Java不解释。只需在Java字符串文本中使用
    \n
    即可获得新行

  • 我还添加了一些代码来读取输出。

    这就是我的工作原理:

    String notifyMsg = "#Genes\nPIK3C2A\nPTEN\nUNC5B";
    String reactome = "https://reactome.org/AnalysisService/identifiers/projection/";
    String notifyTitle = "Content-Type: text/plain";
    
    Process p = new ProcessBuilder("curl", "-H", notifyTitle, "-d", notifyMsg, "-X", "POST", "--url", reactome).start();
    p.waitFor();
    try (Scanner scanner = new Scanner(new InputStreamReader(p.getInputStream()))) {
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    }
    System.out.println("Run Result " + p.exitValue());
    
  • 不要手动控制命令行,请使用
    ProcessBuilder的vararg构造函数
  • 您的
    内容类型
    指令和
    -d
  • $(printf
    是shell解释的东西,但Java不解释。只需在Java字符串文本中使用
    \n
    即可获得新行

  • 我还添加了一些代码来读取输出。

    考虑使用JToice或Apache HTTP组件或其他java库。这里不需要使用CURL。为什么要通过启动<代码> CURL< /CUD>下载文件?当有许多本地java库来执行此操作时,请考虑使用JToSE或Apache HTTP组件或其他JA。va库。您不需要在此处使用curl。当有许多本机Java库可以执行此操作时,为什么要通过启动
    curl
    来下载文件?