【问题标题】:Paths for images and executables in a jarjar 中图像和可执行文件的路径
【发布时间】:2014-09-12 12:22:47
【问题描述】:

我在 Eclipse 中创建了一个 java 程序,现在准备将它导出为 jar。我的程序使用图像文件和可执行文件。在 Eclipse 中测试我的程序时,我使用完整路径引用了这些文件,这显然不能用于 jar。因此,我将它们更改为:

public static final String DRIVERLOC = "./resources/IEDriverServer.exe";
//some other code
File file = new File(DRIVERLOC);
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());

File pic = new File("./images/Open16.gif");
openButton = new JButton("Select the Text File", createImageIcon(pic.getAbsolutePath()));

我把图片和resources和images目录和jar放在同一个目录下。现在由于某种原因,当我运行 jar 时,IEDriverServer 工作正常,但图像不起作用,错误是它找不到图像。我很困惑,因为我似乎无法区分。我还使用了同样不起作用的“images/Open16.gif”。为什么一个可以工作,而另一个不行?解决此问题的最简单方法是什么?

【问题讨论】:

  • 如何执行jar? (从哪里,你用什么命令?)
  • images目录是jar文件的外部还是内部?
  • 我正在使用“java -jar filename”从命令行执行它
  • 图片目录是外部的

标签: java eclipse jar path


【解决方案1】:

我们对 Selenium 驱动程序做同样的事情。 您需要做的是从 jar 中取出可执行文件并将其放在 windows 可以运行它的位置。如果您尝试在 Windows 资源管理器中打开 jar/zip,然后双击 jar/zip 中的 .exe,Windows 会将文件解压缩到临时目录,然后运行它。所以做同样的事情:

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TestClass {
    public static void main (String[] args) throws IOException {
        InputStream exeInputStream = TestClass.class.getClassLoader().getResourceAsStream("resources/IEDriverServer.exe");
        File tempFile = new File("./temp/IEDriverServer.exe");

        OutputStream exeOutputStream = new FileOutputStream(tempFile);
        IOUtils.copy(exeInputStream, exeOutputStream);

        // ./temp/IEDriverServer.exe will be a usable file now.
        System.setProperty("webdriver.ie.driver", tempFile.getAbsolutePath());
    }
}

假设您保存 jar 并使其默认运行这个 main 函数。 运行 C:\code\> java -jar TestClass.jar 将从 C:\code 目录运行 jar。它将在 C:\code\temp\IEDriverServer.exe 中创建可执行文件

【讨论】:

    【解决方案2】:

    当您的路径设置为“./resources/IEDriverServer.exe”时,您指的是硬盘驱动器上的文件“。”这不存在。

    您需要获取 .jar 文件的路径。 您可以使用

    System.getProperty("java.class.path")
    

    这可以返回多个值,以分号分隔。第一个应该是你的 jar 所在的文件夹。

    你也可以使用

    <AnyClass>.class.getProtectionDomain().getCodeSource().getLocation()
    

    我希望这会有所帮助:)

    编辑:

    // First, retrieve the java.class.path property. It returns the location of all jars /  
    // folders (the one that contains your jar and the location of all dependencies)
    // In my test it returend a string with several locations split by a semi-colon, so we
    // split it and only take the first argument
    String jarLocation = System.getProperty("java.class.path").split(";")[0];
    
    // Then we want to add that path to your ressource location
    public static final String DRIVERLOC = jarLocation + "/resources/IEDriverServer.exe";
    //some other code
    File file = new File(DRIVERLOC);
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    

    您可以阅读此here

    【讨论】:

      猜你喜欢
      • 2016-09-15
      • 2018-09-18
      • 1970-01-01
      • 2021-01-10
      • 2020-02-11
      • 1970-01-01
      • 2014-11-24
      • 2019-02-11
      • 1970-01-01
      相关资源
      最近更新 更多