gpt4 book ai didi

java - 从 Tomcat 服务器上的文件夹运行脚本

转载 作者:行者123 更新时间:2023-11-28 23:21:23 25 4
gpt4 key购买 nike

我正在尝试在我的 tomcat 网络服务器上运行脚本。要在我的本地计算机上运行之前的脚本,这是我使用的代码。

String absolutePath = new File(".").getAbsolutePath();
int last = absolutePath.length()-1;
absolutePath = absolutePath.substring(0, last);
String filePath = "";
if(osVersion.equalsIgnoreCase("Ubuntu"))
{
try (FileReader fr = new FileReader("template.txt");
FileWriter fw = new FileWriter("Ubuntu/ubuntu_file.json");) {
int c = fr.read();
while(c!=-1) {
fw.write(c);
c = fr.read();
}
} catch(IOException e) {
e.printStackTrace();
}
filePath = "Ubuntu";
String fi = absolutePath + filePath;
System.out.println(fi);//Get the full path.
// Create ProcessBuilder.
ProcessBuilder pb = new ProcessBuilder("bash", "-c",
"cd "+fi+" ; PACKER_LOG=1 /usr/local/bin/packer build ubuntu_file.json");

Process p = pb.start();

然而,当我尝试在 tomcat 网络服务器上运行它时,我不断收到此错误。

EclipseEE.app/Contents/MacOS/Ubuntu Failed to parse template: open ubuntu_file.json: no such file or directory

我是 Tomcat 的新手,我只是在了解它的来龙去脉。我应该将我的 Ubuntu 文件夹放在哪个 tomcat 目录(我假设它是 webapp 目录),以便 tomcat 获取该文件夹的绝对路径,然后能够运行脚本。

最佳答案

如果您有或多或少传统的 Tomcat 安装,那么 $CATALINA_HOME 环境变量将被设置并指向您的服务器安装,它至少包含以下目录:

$CATALINA_HOME/
bin/
conf/
lib/
webapps/

您可以通过以下方式获取 $CATALINA_HOME 的值:

String catalinaHomeDir = System.getenv("CATALINA_HOME");

我倾向于将您的配置放在 conf 子目录中。

如果你从同一个基础上运行多个 Tomcat 实例,那么一定要阅读它附带的 RUNNING.txt 文件,因为你可能需要使用 $CATALINA_BASE 相反。

当使用 Eclipse 控制的服务器在本地运行时,您可能需要在 Eclipse Tomcat 运行时环境中设置 CATALINA_HOME/BASE

BTW. This is not a portable solution. If you need to migrate to some other container (such as WildFly or Glassfish) then the absolute path config recommended by others is the way to go.

关于java - 从 Tomcat 服务器上的文件夹运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43105493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com