Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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 从jar中的资源运行SH文件_Java_Bash_Jar - Fatal编程技术网

Java 从jar中的资源运行SH文件

Java 从jar中的资源运行SH文件,java,bash,jar,Java,Bash,Jar,我可以运行在JAR中找到的SH文件吗 我正试图从资源中提取该文件,并将其推送到流程中 String fileName = command[0]; ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource(fileName).getFile());

我可以运行在JAR中找到的SH文件吗

我正试图从资源中提取该文件,并将其推送到流程中

                String fileName = command[0];
                ClassLoader classLoader = getClass().getClassLoader();

                File file = new File(classLoader.getResource(fileName).getFile());

                List<String> temp = new ArrayList<>();

                temp.add("sh");
                temp.add(file.getPath());
                temp.addAll(Arrays.stream(command).filter(s -> !s.contains(".sh")).collect(Collectors.toList()));

                String[] stringExec = new String[temp.size()];

                for (int i = 0; i < stringExec.length; i++)
                    stringExec[i] = temp.get(i);

//                Process process = Runtime.getRuntime().exec(stringExec);


                Path script = Files.createTempFile(null, ".sh",
                        PosixFilePermissions.asFileAttribute(
                                PosixFilePermissions.fromString("rw-rw-r--")));

                    Files.copy(file.toPath(), script, StandardCopyOption.REPLACE_EXISTING);

                String[] cmd = { "bash", script.toString() };

                ProcessBuilder builder = new ProcessBuilder(cmd);
                builder.inheritIO();

                Process process = builder.start();
                process.waitFor();

                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                BufferedReader stdError = new BufferedReader(new
                        InputStreamReader(process.getErrorStream()));

                String line;
                while ((line = reader.readLine()) != null) {
                    CustomLog.log(line, TypeLog.LOG);
                }

                while ((line = stdError.readLine()) != null) {
                    CustomLog.log(line, TypeLog.WARN);
                }

                Files.delete(script);

或者只是如果外部文件,我就可以执行它?

我怀疑
文件。copy
将从jar中复制文件。我建议使用
ClassLoader.getressourceastream
并将流复制到临时文件

java.nio.file.NoSuchFileException: file:/root/Documents/module.jar!/file.sh