JavaFX程序无法在它运行';部署

JavaFX程序无法在它运行';部署,java,javafx,javafx-8,Java,Javafx,Javafx 8,我编写了一个JavaFX程序,它运行一个python脚本,该脚本使用google控制台搜索api获取数据。在我将其作为独立程序(.exe)部署之前,它工作正常。安装程序后,它运行良好,直到我单击运行脚本的按钮;它什么也不做 单击按钮后,它会成功地检查python是否已安装,然后在尝试运行脚本时出现问题。我已经像这样指定了路径,“/src/com/fortunaaritime/”,我认为这可能就是问题所在 private static final String ANALYTICS_PATH =".

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

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

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文件,以查看发生了什么,只有一行写着“系统找不到指定的路径”


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

this.getClass().getResource("/com/fortunamaritime/analytics.py")
结果是:

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

是否可以访问jar文件的内部,并通过cmd通过剥离此URL的部分来运行脚本?

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

您可以使用
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/fortunaaritime/analytics.py com/fortunaaritime/client_secrets.json com/fortunaaritime/webmasters.dat


然后我可以让它工作。

这确实很可能是问题所在。源文件夹中的文件很少添加到已部署的程序中,如果是,则通常作为资源,而不是文件系统的一部分。工作目录中是否有
/src/com/fortunaaritime/
(通常是
.exe
的位置)启动程序时?我建议您检查是否安装了python,如果没有安装,则显示错误消息。我将使用
String.join()
而不是
cmd[0]+“”+…
您可以使用nio(或io)从jar复制它我已经在我的答案中添加了一个代码示例。我想我可能已经采取了不实用的方法,但无论如何还是要谢谢你