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文件放在“scripts”目录下并执行它。我的程序能够在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
答案 0 :(得分:0)
您是否看到了org.apache.commons.io.FilenameUtils
的课程Apache Commons IO。方法:
public static String separatorsToSystem(String path)
将所有分隔符转换为系统分隔符。