gpt4 book ai didi

java - 从线程执行时相对路径被更改

转载 作者:行者123 更新时间:2023-12-01 05:22:49 27 4
gpt4 key购买 nike

我正在制作一个 Web 应用程序,它在某个时刻启动一个新线程,并且该线程从命令行执行一个 jar 文件。当从应用程序外部调用时,命令行 jar 工作正常,但是当我从线程调用它时,相对路径变为 C:\eclipse\(我从 Eclipse 运行应用程序)而不是它存储的目录,这会造成困惑调整其配置,因为它在错误的位置查找文件。

jar 会创建一个日志文件,每当我尝试调用它时,我都会在日志中写入以下行:“10/04/2012 17:09:03 - java.io.FileNotFoundException: C:\eclipse\descriptors\Analysis_engine\AggregateAE.xml"该 jar 不在 C:\eclipse 内。当我从提示符调用它时,我没有问题,但是当从新生成的线程调用它时,我遇到此错误。我在生产环境上尝试过,也有同样的问题(这次基本路径是服务器的路径)

考虑到我无法修改所有路径,这个问题的解决方案是什么?

编辑:这是调用 jar 的线程类

public class UimaThread extends Thread {
private int mode=0;
private String path;

public UimaThread(int mode, String path){
this.mode=mode;
this.path=path;
}

public void run() {
Runtime run = Runtime.getRuntime();
try {
Properties config = ConfigLoader.getConfig();

String uimaPath=config.getProperty("uimaPath")+ControlPanelUtils.getDelimiter(config.getProperty("uimaPath"));
//uimaPath is the absolute path to the jar file, mode and path are just arguments passed to the jar
run.exec("java -jar "+uimaPath+"uimachainfull.jar "+mode+" "+path);

}

}

运行此代码的代码是:

public void startUima() throws IOException, ServletException {
Properties config = ConfigLoader.getConfig();
UimaThread uimaThread = new UimaThread(2, config.getProperty("docPath"));
uimaThread.start();
}

我需要在服务器外部异步执行此操作,我已在 stackoverflow 中询问如何执行此操作,我被告知这样做:Calling an application from a web server asynchronously

最佳答案

我已经发现了如何做到这一点,而不是使用 exec(String command) 我必须使用 exec(String command, String[] envp, File dir) 并且最后一个参数 (dir) 是可以的工作目录作为新参数传递

关于java - 从线程执行时相对路径被更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10091800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com