部署后,JavaFX 程序无法 运行 python 脚本

JavaFX program cannot run python script after it's deployed

我编写了一个 JavaFX 程序,它 运行 是一个 python 使用 google 控制台搜索 api 获取数据的脚本。在我将其部署为独立程序 (.exe) 之前,它运行良好。安装程序后,运行 一切正常,直到我单击 运行 脚本按钮;它什么都不做。

单击按钮后,它会成功检查是否安装了 python,然后在尝试 运行 脚本时出现问题。我已经指定了这样的路径,“./src/com/fortunamaritime/”,我认为这可能是问题所在。

private static final String ANALYTICS_PATH ="./src/com/fortunamaritime/";

方法内部

//set up the command and parameter
String[] cmd = new String[3];
cmd[0] = "python"; 
cmd[1] = "analytics.py";
cmd[2] = "https://fortunamaritime.com.tr";
String command = cmd[0] + " " + cmd[1] + " " + cmd[2] + " " + 
startDate + " " + endDate;
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "cd " + new 
File(ANALYTICS_PATH).getAbsolutePath() + " && " + command);
builder.redirectErrorStream(true);
Process p = builder.start();

我已将控制台输出重定向到一个 .txt 文件以查看发生了什么,只有一行显示 "system cannot find the path specified"。


我已经从绝对路径切换到相对路径,但它产生了这个错误: 文件名、目录名或卷标语法不正确。 我还将这一行打印到控制台

this.getClass().getResource("/com/fortunamaritime/analytics.py")

结果是:

jar:file:/D:/IdeaProjects/FortunaWebTracker/out/artifacts/FortunaWebTracker/FortunaWebTracker.jar!/com/fortunamaritime/analytics.py

是否可以通过 cmd 通过剥离此 URL 的部分内容从那里访问 jar 文件和 运行 脚本?

将脚本复制到一个(可能是临时的)文件,然后从该文件运行复制它。

您可以使用 Main.class.getResourceAsStream(String resc)(相对于 main)或 Main.class.getClassLoader().getResourceAsStream(String resc)(相对于 root)

获取作为 InputStream 的脚本

您可以使用 java.nio.file.Files#createTempFile(String,String 并在退出时删除该文件。

您也可以将脚本复制到用户主目录或相对于 exe(不要删除它)。

例如,您可以这样做:

File dir=new File("./resc");
if(!dir.exists()) {
    dir.mkdirs();
}
File analyticsFile=new File(dir,"analytics.py");
if(!analyticsFile.exists()) {
    /*
     * alternative:
     * this.getClass().getResourceAsStream("com/fortunamaritime/analytics.py")
     */
    try(InputStream analyticsIn = this.getClass().getClassLoader().getResourceAsStream("analytics.py")){
        Files.copy(analyticsIn, analyticsFile.toPath());
    }
}
/*
 * alternative: String command=String.join(" ",new String[]{"python",analyticsFile.getAbsolutePath(),"https://fortunamaritime.com.tr"});
 */
String command="python "+analyticsFile.getAbsolutePath()+" https://fortunamaritime.com.tr";
/*
 * alternative (without error redirection): Process p=Runtime.getRuntime().exec(command);
 */
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
Process p=builder.start();

我的问题已经解决了。 先上代码示例,后面会有解释;

String path =  new 
File(Main.class.getProtectionDomain().getCodeSource().getLocation()
     .toURI()).getPath();
path=path.substring(0,path.length()-21);
// set up the command and parameter
String[] cmd = new String[3];
cmd[0] = "python";
cmd[1] = "analytics.py";
cmd[2] = "https://fortunamaritime.com.tr";
String command0="jar xf FortunaWebTracker.jar" +                   
"com/fortunamaritime/analytics.pycom/fortunamaritime/client_secrets.json" +
" com/fortunamaritime/webmasters.dat";
String command1 = cmd[0] + " " + cmd[1] + " " + cmd[2] + " " + startDate + " " + 
endDate;
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "cd " + path + " && " + 
command0+"&&"+"cd "+path+"\com\fortunamaritime"+" && "+command1);
builder.redirectErrorStream(true);
Process p = builder.start();

我的第一个错误是使用绝对路径,第二个问题是当我通过以下方式获得我想要的路径时出现的:

File(Main.class.getProtectionDomain().getCodeSource().getLocation()
     .toURI()).getPath();
//Jar's name is 21 characters long, minus that and I get the directory path
path=path.substring(0,path.length()-21);

第二个问题是解压 jar 中的元素,所以我在 cmd 中使用了这个命令:

jar xf FortunaWebTracker.jar
com/fortunamaritime/analytics.py com/fortunamaritime/client_secrets.json com/fortunamaritime/webmasters.dat

然后我就可以让它工作了。