gpt4 book ai didi

java - 是否可以通过路径访问 jar 内的资源?

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:07 25 4
gpt4 key购买 nike

我有一个 SoapUI 项目,通过一个 java 项目运行,该项目全部打包到一个 jar 中。该项目在我的 IDE 中解包后运行良好,但在通过命令行运行 jar 时遇到问题。这是导致问题的代码:

SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
ClassLoader cl = MyClass.class.getClassLoader();
runner.setProjectFile(cl.getResource("project.xml").getFile());
runner.run();

当 run 方法尝试从 jar 中读取 xml 文件时,会出现问题。我是否可以获得可以输入到 setProjectFile 的路径 jar 里的方法?我看到其他答案说使用流,但此方法仅接受字符串。

这是堆栈跟踪:

2014-07-23 12:03:58,384 ERROR [errorlog] com.eviware.soapui.support.SoapUIException: Failed to load project from file [file:/C:/temp/MyJar.jar!/project.xml] com.eviware.soapui.support.SoapUIException: Failed to load project from file [file:/C:/temp/MyJar.jar!/project.xml] at com.eviware.soapui.impl.wsdl.WsdlProject.loadProject(WsdlProject.java:315) at com.eviware.soapui.impl.wsdl.WsdlProject.(WsdlProject.java:231) at com.eviware.soapui.impl.wsdl.WsdlProjectFactory.createNew(WsdlProjectFactory.java:41) at com.eviware.soapui.impl.wsdl.WsdlProjectFactory.createNew(WsdlProjectFactory.java:28) at com.eviware.soapui.tools.SoapUITestCaseRunner.runRunner(SoapUITestCaseRunner.java:329) at com.eviware.soapui.tools.AbstractSoapUIRunner.run(AbstractSoapUIRunner.java:188) at CommercialTests.ComTest.doTest(ComTest.java:14) at CommercialTests.ServiceTests.main(ServiceTests.java:8) Caused by: java.io.FileNotFoundException: C:\temp\MyJar.jar!\project.xml (The system cannot find the path specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source) at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source) at java.net.URL.openStream(Unknown Source) at com.eviware.soapui.impl.wsdl.support.wsdl.UrlWsdlLoader.handleFile(UrlWsdlLoader.java:180) at com.eviware.soapui.impl.wsdl.support.wsdl.UrlWsdlLoader.load(UrlWsdlLoader.java:116) at com.eviware.soapui.impl.wsdl.support.wsdl.UrlWsdlLoader.load(UrlWsdlLoader.java:96) at com.eviware.soapui.impl.wsdl.WsdlProject.loadProject(WsdlProject.java:297) ... 7 more

最佳答案

对于这样的事情我通常会这样做:

URL res = getClass().getResource("/project.xml")    // is it at the root of your jar?!?!
File f = new File(res.getFile())
runner.setProjectFile(f.getCanonicalPath());

SoapUI,根据它访问文件的方式,可能在“真实”世界中需要它。下面是上述版本的资源密集型版本。

// locate your file in the jar resources
URL res = getClass().getResource("/project.xml") // is it at the root of your jar?!?!
// locate the resource in the filesystem
File f = new File(res.getFile())
// copy the file out into the real filesystem
File target = new File(System.getProperty("java.io.tmpdir") + File.separator + f.getName())
java.nio.file.Files.copy(f.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING)
runner.setProjectFile(target.getCanonicalPath())

关于java - 是否可以通过路径访问 jar 内的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24916770/

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