我想在我的tomcat web服务器上运行脚本。要在本地机器上运行脚本,这是我使用的代码。从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 web服务器上运行它时,我不断收到此错误。
EclipseEE.app/Contents/MacOS/Ubuntu解析失败模板:开 ubuntu_file.json:没有这样的文件或目录
我是相当新的Tomcat和我刚学它的来来去去。什么tomcat目录应该放置我的Ubuntu文件夹(我假设它是webapp目录),以便tomcat获取文件夹的绝对路径,然后能够运行该脚本。
您不应该依赖Web服务器的当前目录(例如Tomcat)。始终在Web应用程序代码中使用完全限定的文件名。 – Andreas
如果这听起来很愚蠢,我很抱歉。我对这一切都很陌生。那么我应该怎么做呢?它原本不应该是一个网络应用程序。 – user3078335
如果您不想对合格的文件名进行硬编码(这不是一个好主意),这意味着您的web应用程序需要配置*的能力,例如使用[servlet初始化参数](http://stackoverflow.com/q/14665037/5221149)。 – Andreas