Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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 ProcessBuilder在资源文件夹-Spring Boot中找不到sh文件_Java_Spring_Spring Boot_Processbuilder - Fatal编程技术网

Java ProcessBuilder在资源文件夹-Spring Boot中找不到sh文件

Java ProcessBuilder在资源文件夹-Spring Boot中找不到sh文件,java,spring,spring-boot,processbuilder,Java,Spring,Spring Boot,Processbuilder,我已经在互联网上搜索了一段时间的解决方案,但没有一个给出sh文件将如何执行的清晰图像 我有一个shell脚本install.sh,保存在resources目录中。我想从ProcessBuilder运行这个。不管我怎么做,我总是得到一个没有这样的文件或目录的错误。这是我的代码: String masterURL = config.getMasterUrl(); String adminToken = config.getAdminToken(); ProcessBuilder installSc

我已经在互联网上搜索了一段时间的解决方案,但没有一个给出sh文件将如何执行的清晰图像

我有一个shell脚本
install.sh
,保存在
resources
目录中。我想从ProcessBuilder运行这个。不管我怎么做,我总是得到一个
没有这样的文件或目录的错误。这是我的代码:

String masterURL = config.getMasterUrl();
String adminToken = config.getAdminToken();

ProcessBuilder installScriptBuilder = new ProcessBuilder();
installScriptBuilder.command("install.sh", dir, namespace);
installScriptBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);

Map<String,String> installEnv = installScriptBuilder.environment();
installEnv.put("URL", masterURL);
installEnv.put("TOKEN", adminToken);

try {
    Process p = installScriptBuilder.start();
} catch (IOException e) {
    e.printStackTrace();
}
String masterURL=config.getMasterUrl();
字符串adminToken=config.getAdminToken();
ProcessBuilder installScriptBuilder=新建ProcessBuilder();
installScriptBuilder.command(“install.sh”,dir,命名空间);
installScriptBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Map installEnv=installScriptBuilder.environment();
installEnv.put(“URL”,masterURL);
installEnv.put(“令牌”,adminToken);
试一试{
进程p=installScriptBuilder.start();
}捕获(IOE异常){
e、 printStackTrace();
}

我在其他答案中读过关于创建临时文件的内容,但没有一个明确说明如何解决这个问题。我已经使用Spring初始化器创建了我的项目。

您可以使用“classpath:install.sh”选择资源文件

在这种情况下,我们不能这样做。我们需要路径本身:

final ClassLoader classLoader = getClass().getClassLoader();
    final File file = new File(classLoader.getResource("install.sh").getFile());
    final ProcessBuilder installScriptBuilder = new ProcessBuilder();
    installScriptBuilder.command(file.getPath());

您可以使用“classpath:install.sh”选择资源文件

在这种情况下,我们不能这样做。我们需要路径本身:

final ClassLoader classLoader = getClass().getClassLoader();
    final File file = new File(classLoader.getResource("install.sh").getFile());
    final ProcessBuilder installScriptBuilder = new ProcessBuilder();
    installScriptBuilder.command(file.getPath());

资源
文件夹未在目标中维护。这是maven的文件夹结构。您可能需要查看
target
文件夹以了解
install.sh
文件的位置。它将可以在classpath的
根目录下访问
资源
文件夹未在目标中维护。这是maven的文件夹结构。您可能需要查看
target
文件夹以了解
install.sh
文件的位置。它可以在类路径的
根目录下访问
资源位于类路径上,通常压缩到jar中,因此是只读的,而不是文件系统上的文件

Path path = dir.toPath().resolve("install.sh"); // dir a File
Path path = Paths.get(dir, "install.sh"); // dir a String
InputStream in = getClass().getResourceAsStream("/install.sh");
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
使用资源作为模板将其复制到文件系统

Path path = dir.toPath().resolve("install.sh"); // dir a File
Path path = Paths.get(dir, "install.sh"); // dir a String
InputStream in = getClass().getResourceAsStream("/install.sh");
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);

资源位于类路径上,通常压缩到jar中,因此是只读的,而不是文件系统中的文件

Path path = dir.toPath().resolve("install.sh"); // dir a File
Path path = Paths.get(dir, "install.sh"); // dir a String
InputStream in = getClass().getResourceAsStream("/install.sh");
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
使用资源作为模板将其复制到文件系统

Path path = dir.toPath().resolve("install.sh"); // dir a File
Path path = Paths.get(dir, "install.sh"); // dir a String
InputStream in = getClass().getResourceAsStream("/install.sh");
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);

我相信这一行应该有效:

installScriptBuilder.command(new ClassPathResource("install.sh").getPath());

我相信这一行应该有效:

installScriptBuilder.command(new ClassPathResource("install.sh").getPath());

无法运行程序“classpath:install.sh”:错误=2,没有这样的文件或目录你测试过这个想法吗?@piyushrivastava我没跟你说话。java.io.IOException:无法运行程序“install.sh”(在目录“classpath:install.sh”中):错误=2,没有这样的文件或目录;添加目录后,我收到一个权限被拒绝的错误:
无法运行程序/home/piyush/workspace/service/target/classes/install.sh”:错误=13,权限被拒绝
无法运行程序“classpath:install.sh”:错误=2,没有这样的文件或目录你测试过这个想法吗?@piyushrivastava我没有和你说话。java.io.IOException:无法运行程序“install.sh”(在目录“classpath:install.sh”中):错误=2,没有这样的文件或目录;添加目录后,我收到一个权限被拒绝的错误:
无法运行程序/home/piyush/workspace/service/target/classes/install.sh”:错误=13,权限被拒绝
我在/tmp目录中复制了该文件。我可以看到它被复制到了文件系统中。然后我设置
installScriptBuilder.directory(新文件(“/tmp”)。。但是仍然得到同样的错误
无法运行程序“install.sh”(在目录“/tmp”中):错误=2,没有这样的文件或目录
文件权限(
chmod
)和文件所有者(
chown
)?虽然/tmp通常具有DRWXRWXT权限,但脚本具有-rw-rw-r--。虽然我已经在
resources
executable中创建了原始脚本。如何使新文件可执行并可供java访问?我在/tmp目录中复制了该文件。我可以看到它被复制到了文件系统中。然后我设置
installScriptBuilder.directory(新文件(“/tmp”)。。但是仍然得到同样的错误
无法运行程序“install.sh”(在目录“/tmp”中):错误=2,没有这样的文件或目录
文件权限(
chmod
)和文件所有者(
chown
)?虽然/tmp通常具有DRWXRWXT权限,但脚本具有-rw-rw-r--。虽然我已经在
resources
executable中创建了原始脚本。如何使新文件可执行并可供java访问?