简体   繁体   English

错误:启动进程构建器命令时无法访问 jarfile

[英]Error: Unable to access jarfile when I launch a process builder command

I build one program in Java with a main into a jarfile and I want to executable an other.我在 Java 中构建了一个程序,其中一个 main 到一个 jarfile 中,我想执行另一个程序。 The second program is also a jarfile.第二个程序也是一个jarfile。

To execute the second program, I use a process builder to execute the second jarfile with these arguments.为了执行第二个程序,我使用进程构建器来执行带有这些 arguments 的第二个 jarfile。 But i didn't worked and I have this message:但我没有工作,我有这个消息:

Error: Unable to access jarfile path/second_program.jar错误:无法访问 jarfile 路径/second_program.jar

In first, I think its the path was wrong.首先,我认为它的路径是错误的。 But, I have execute the second jarfile in a terminal on windows and on linux whitout any problem.但是,我已经在 windows 和 linux 的终端上执行了第二个 jarfile,没有任何问题。


//IN THE FIRST PROGRAM
                Process process = null;
                List<String> commands = new ArrayList<String>();

                if (server.equals("TEST_linux")) {
                    commands.add("/bin/java");
                    commands.add("-jar");
                    commands.add("/mnt/c/Users/second_program.jar");
                    commands.add("param1");
                    commands.add("param2");
                } else {
                    commands.add("java");
                    commands.add("-jar");
                    commands.add("C:/Users/second_program.jar");
                    commands.add("param1");
                    commands.add("param2");                    
                }
                System.out.println(commands);
                //Execution de la commande externe
                ProcessBuilder pb = new ProcessBuilder(commands);
                System.out.println(pb.directory());
                pb.directory(new File(localFolder));
                System.out.println(pb.directory());

                pb.redirectErrorStream(true);
                process = pb.start();
                StringBuilder out = new StringBuilder();
                BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null, previous = null;

                file_generated_by_second_program = output_folder + "/" + panel + "/file_generated_by_second_program.txt";

                myFile = new File(file_generated_by_second_program);
                writer = new BufferedWriter(new FileWriter(myFile, true));
                System.out.println(br.readLine());

All of the rest of the first program is executing perfectly.第一个程序的所有 rest 都在完美执行。 I use the first program which generated arguments to execute the second program which generate a file.我使用生成 arguments 的第一个程序来执行生成文件的第二个程序。 This file is used after in the first program... I can't launch the second program separatly...该文件在第一个程序中使用后...我无法单独启动第二个程序...

Any ideas?有任何想法吗?

UPDATE:更新:

Here, these errors:在这里,这些错误:

//Windows before suggestions :
Error: Unable to access jarfile C:/Users/second_program.jar

//Windows after suggestions : 
Error: Unable to access jarfile C:\Users\second_program.jar

//Linux :
Error: Unable to access jarfile "/mnt/c/Users/second_program.jar"

System.out.println(br.readLine()); //==> this command doesn't execute too!

After the last row of my code, I create differents variables and these variables existing and there are no null.在我的代码的最后一行之后,我创建了不同的变量并且这些变量存在并且没有 null。 So for me, the problem is limited to the extract of my code...所以对我来说,问题仅限于我的代码的提取......

SOLVED: I had an error in path...sorry已解决:我的路径有误...对不起

This is related to different path separator characters:这与不同的路径分隔符有关:

  • Windows is using "\" Windows 正在使用“\”
  • Linux is using "/" Linux 正在使用“/”

Usually, following problem can be solved by using File.separator property, which will return the right character basing on OS that you are running.通常,可以通过使用File.separator属性来解决以下问题,该属性将根据您正在运行的操作系统返回正确的字符。 However, i can see that you are most likely using WSL, so it might not work in this case and you would have to hard-code this.但是,我可以看到您最有可能使用 WSL,因此在这种情况下它可能不起作用,您必须对其进行硬编码。

Summing up, you need to fix path to jar for Windows and differentiate output paths for both OS.总而言之,您需要为 Windows 修复 jar 的路径,并区分两个操作系统的 output 路径。

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

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