Runnable Jar文件中没有此类文件异常

时间:2016-10-11 09:38:27

标签: java

我正在尝试创建一个可运行的jar文件。我的项目包括models.txt文件。我的项目在eclipse中完美运行,没有错误,但是当导出到可运行的jar文件时,它不起作用。我特此附上错误和调用文件的代码段。

public static HashMap<String, RenderModel>  getModelList(String file) throws IOException {
    List<String> data;
    HashMap<String, RenderModel> namesToModels = new HashMap<String, RenderModel>();

    if (file != null) {
        data = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8);
    } else {
        String path = "models/models.txt";
        data = Files.readAllLines(Paths.get(path),  StandardCharsets.UTF_8);
    }

    Iterator<String> dataIterator = data.iterator();
    while (dataIterator.hasNext()) {
        String dataLine = dataIterator.next();
        System.out.println(dataLine);
        String[] line = dataLine.split("; ");
        String key = line[0];
        String valueObj = line[1];
        String valueMtl = line[2];
        float scale = Float.parseFloat((String) line[3]);
        RenderModel v = new RenderModel(valueObj, valueMtl, scale);
        namesToModels.put(key, v);
    }
    RenderModel v =  new RenderModel("custom", "custom", 1.0f);
    namesToModels.put("Choose Model from file", v);
    return namesToModels;
}

错误图片:

Error Image

3 个答案:

答案 0 :(得分:1)

如果文件位于Jar但您无法阅读,请尝试访问这些文件:

getClass().getClassLoader().getResource(fileName);

使用此代替静态方法:

ClassName.class.getClassLoader().getResource(fileName);

其中fileName是文件的名称,ClassName是从中调用语句的类的名称。

答案 1 :(得分:0)

在您的代码中,model.txt的路径是&#39; src / models / model.txt&#39;。打包项目时,通常不包含src文件夹。然后你必须改变文件位置;可以更好地将文件放在jar之外,但是在java类路径中。

答案 2 :(得分:0)

它不起作用,因为当你在其他地方运行你的jar时路径#include <vector> #include <fstream> struct MyStruct{ int x; float y; float array[100]; MyStruct(){ x = 0; y = 1.0; for(int i=0; i<100; i++){ array[i] = i; } } void print(){ std::cout << x << " " << y << std::endl; for(int i=0; i<100; i++){ std::cout << i << " "; } } }; template<typename T> void write_pod(std::ofstream& out, T& t) { out.write(reinterpret_cast<char*>(&t), sizeof(T)); } template<typename T> void read_pod(std::ifstream& in, T& t) { in.read(reinterpret_cast<char*>(&t), sizeof(T)); } template<typename T> void write_pod_vector(std::ofstream& out, std::vector<T>& vect) { long size = vect.size(); write_pod<long>(out, size); out.write(reinterpret_cast<char*>(&vect.front()), size * sizeof(T)); } template<typename T> void read_pod_vector(std::ifstream& in, std::vector<T>& vect) { long size; read_pod(in, size); vect.resize(size); in.read(reinterpret_cast<char*>(&vect.front()), size * sizeof(T)); } int main(int argc, char **argv) { ros::init(argc, argv, "weighing_area"); std::vector<MyStruct> testArray; testArray.push_back(MyStruct()); testArray.push_back(MyStruct()); ofstream myfile("/home/me/TEST.dat"); write_pod_vector<MyStruct>(myfile, testArray); myfile.close(); std::vector<MyStruct> readArray; ifstream readfile("/home/me/TEST.dat"); read_pod_vector(readfile, readArray); cout << readArray.size() << endl; for(int i=0; i<readArray.size(); i++){ MyStruct& myStruct = readArray[i]; myStruct.print(); } return 0; } 上没有任何文件,这条路径只存在于你的IDE中(当然你可以将你的jar放在一个位置它可以到达那条路径,但这不是它应该如何),当你将项目打包到一个jar文件中时,它被打包在包src/models/models.txt中,如果你想把它作为默认文件你可以通过classpath阅读。

相关问题