Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jar中图像和可执行文件的路径_Java_Eclipse_Jar_Path - Fatal编程技术网

Java jar中图像和可执行文件的路径

Java jar中图像和可执行文件的路径,java,eclipse,jar,path,Java,Eclipse,Jar,Path,我已经在eclipse中创建了一个java程序,现在准备将其导出为jar。我的程序使用图像文件和可执行文件。在eclipse中测试我的程序时,我使用完整路径引用了这些文件,这显然不能用于jar。所以我就这样改了, public static final String DRIVERLOC = "./resources/IEDriverServer.exe"; //some other code File file = new File(DRIVERLOC); System.setProperty(

我已经在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());

我将图像、资源和图像目录与jar放在同一个目录中。现在,由于某种原因,当我运行jar时,IEDriverServer工作正常,但是映像不工作,错误是它找不到映像。我很困惑,因为我似乎分不清两者之间的区别。我还使用了“images/Open16.gif”,这也不起作用。为什么一个能工作而另一个不能?解决此问题的最简单方法是什么?

将路径设置为“/resources/IEDriverServer.exe”时,您指的是硬盘上不存在的文件

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

System.getProperty("java.class.path")
这可以返回多个值,用分号分隔。第一个应该是jar所在的文件夹

你也可以使用

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

您可以阅读这篇文章,我们对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,并使其在默认情况下运行这个主函数。
运行
C:\code\>java-jartestclass.jar
将从C:\code目录运行jar。它将在C:\code\temp\IEDriverServer.exe创建可执行文件

如何执行jar?(从哪里,您使用什么命令?)images目录是jar文件的外部目录还是内部目录?这几乎是我在命令行中使用“java-jarfilename”执行它的副本。images目录是外部目录
// 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());
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());
    }
}