【问题标题】:Tomcat webapp's file path under multiple OS多操作系统下tomcat webapp的文件路径
【发布时间】:2013-09-18 13:58:36
【问题描述】:

Java 代码:

//Gives the webapp directory
String pythonScriptPath =  getServletContext().getRealPath(File.separator);
//Gives OS name
String OS = System.getProperty("os.name").toLowerCase();

if (OS.indexOf("win") >= 0) {
pythonScriptPath = pythonScriptPath + "scripts\\data_parser.py";
} else if ((OS.indexOf("mac") >= 0) {
    pythonScriptPath = pythonScriptPath + "scripts/data_parser.py";
}

String[] cmd = new String[3];
cmd[0] = "python";
cmd[1] = pythonScriptPath;
cmd[2] = "2013-09-10T08:00:00-04:00";

// create runtime to execute external command
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);

这段代码在 Mac 机器上运行良好。问题出在windows机器上。我正在尝试获取“脚本”目录下的 python 文件并执行它。我的程序能够在 Mac 中找到该文件,但在 Windows 中找不到。

Windows下的文件:C:\Users\Administrator\TEST.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\TEST\scripts\data_parser.py

Mac 下的文件: /Users/satishjonnala/TEST/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/TEST/scripts/data_parser.py

【问题讨论】:

  • 你试过 os.path.join(pythonScriptPath, "scripts", "data_parser.py") 吗?
  • 你遇到了什么异常?
  • 我是否需要以不同的方式从 Windows 系统运行 Python 程序。我删除了 "#!/usr/bin/env python" 并更新了 "cmd[0] = "C:\\Python\\python";"从 Windows 运行。它显示来自 TomCat 的“exception.WindowsError”
  • @aglassman,我得到如下 - localhost:8080 的页面显示:数据:2013-09-10T08:00:00-04:00 问题:

标签: java python eclipse macos tomcat


【解决方案1】:

你看到Apache Commons IO的类org.apache.commons.io.FilenameUtils了吗。方法:

public static String separatorsToSystem(String path)

将所有分隔符转换为系统分隔符。

【讨论】:

  • 我认为它能够找到正确的路径。但是在运行我的 python 脚本窗体 windows 程序时存在问题。不过谢谢你的建议。
猜你喜欢
  • 2015-07-16
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 2012-03-01
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
相关资源
最近更新 更多