简体   繁体   English

Java.io.IOException:错误= 2,在Java中执行curl时没有此类文件或目录

[英]Java.io.IOException: error=2, No such file or directory on executing curl in java

I'm using curl for certificate-based/x509 authentication ` 我正在使用curl进行基于证书的/ x509身份验证

String command = "curl --cert " +certificateFilePath+" --key "+ certificateKeyFilePath+" --insecure "+authUrl;

          ProcessBuilder process = new ProcessBuilder(command); 
            Process p;
            try
            {
                p = process.start();
                 BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line = null;
                    while ( (line = reader.readLine()) != null) {
                            builder.append(line);
                            builder.append(System.getProperty("line.separator"));
                    }
                    String result = builder.toString();
                    System.out.print("result ------------------:  "+result);

            }catch(Exception e){
                e.printStackTrace();
            }

` `

but getting exception ` 但得到例外

Caused by: java.io.IOException: error=2, No such file or directory
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
        at java.lang.ProcessImpl.start(ProcessImpl.java:134)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)

` `

given that the application and auth service are running inside docker-container on Ubuntu and works fine when I manually go inside the application docker-container and execute the above curl command. 鉴于应用程序和身份验证服务在Ubuntu上的docker-container内部运行,并且当我手动进入应用程序docker-container并执行上述curl命令时,可以正常工作。 Please help 请帮忙

From process builder docs: 从流程构建器文档中:

https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

The proper way to do it is to call it with the parameters passed as separate strings otherwise it won't work. 正确的方法是使用作为单独字符串传递的参数来调用它,否则它将不起作用。 Also in order to run system commands like curl in linux or dir in windows you need to call the command interpreter - sh for linux. 同样,为了在linux中运行curl或在Windows中运行dir等系统命令,您需要调用命令解释程序-sh for linux。 So your command should become something like: 因此,您的命令应如下所示:

ProcessBuilder process = new ProcessBuilder("sh", "-c","curl","--cert", certificationPath......); 

Or you can try: 或者您可以尝试:

Runtime.getRuntime().exec(command);  //Here you pass the whole command as a string  

您是否可以尝试在root用户下或使用sudo java运行JVM。...它可能没有足够的JVM权限。

暂无
暂无

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

相关问题 java.io.IOException: error=2, 没有那个文件或目录 - java.io.IOException: error=2, No such file or directory java.io.IOException: 无法运行程序“...”: java.io.IOException: error=2, No such file or directory - java.io.IOException: Cannot run program “…”: java.io.IOException: error=2, No such file or directory Java,由:java.io.IOException: error=2, No such file or directory - Java, Caused by: java.io.IOException: error=2, No such file or directory File.createTempFile(错误:java.io.IOException:没有这样的文件或目录) - File.createTempFile (Error: java.io.IOException: No such file or directory) 错误:java.io.IOException:android 11 中没有此类文件或目录 - Error: java.io.IOException: No such file or directory in android 11 java.io.IOException:无法运行程序“/usr/bin/sh”:java.io.IOException:error=2,没有那个文件或目录 - java.io.IOException: Cannot run program “/usr/bin/sh”: java.io.IOException: error=2, No such file or directory java.io.IOException:无法运行程序“ C:\\ AutoIt \\ ModenaAutoIt.exe”:java.io.IOException:error = 2,没有此类文件或目录 - java.io.IOException: Cannot run program “C:\AutoIt\ModenaAutoIt.exe”: java.io.IOException: error=2, No such file or directory java.io.IOException:java.io.FileNotFoundException :(无此类文件或目录) - java.io.IOException: java.io.FileNotFoundException:(No such file or directory) 无法制作文件 java.io.IOException: 没有这样的文件或目录 - Cannot make file java.io.IOException: No such file or directory 无法启动测试系统&#39;slim&#39;:java.io.IOException:无法运行程序“ java”:error = 2,没有这样的文件或目录 - Unable to start test system 'slim': java.io.IOException: Cannot run program “java”: error=2, No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM