简体   繁体   中英

Runnable JAR file not found exception

I would like to know how to create a runnable JAR with resources (pictures, pdfs) in a resource folder either inside or outside the source package (ie /src/resources/images/ or /resources/images/) in Eclipse. Currently, i have my resources inside the source folder of my eclipse project, but I've also tried it inside its own folder in the package. The program builds and executes fine in eclipse, but when I go to export as a runnable jar, I keep getting a file not found exception when I run it on the desktop. I'm declaring my files with a string like this

private String file =  "src/resources/orderForm.pdf";

I understand that I should user getResourceAsStream(), but due to some constraints, I can't (has to do with how files are saved, I'm reading in whole pdf files, not as streams) so I'm wondering how to get my files into the correct location in the jar. If i unpack it after I've made it they always show up in the top level, outside of the folders. Here is a screen shot of my current project structure. For the sake of saying it, this project works fine in eclipse, also in the java build properties, the source folder is in the build path along with all subfolders, I also tried doing the same with the empty resources folder in an earlier test. 在此输入图像描述

You will need to do the getResourceAsStream, and make sure that the pdfs get built into the jar. Since you have them in the source folder, they should.

Assuming you have the pdf under src/resources/orderForm.pdf, it will end up in the jar file as /resources/orderForm.pdf. You would open a resource stream for /resources/orderForm.pdf.

If you must have a honest to goodness file, then you would need code that reads the PDF as a resource stream, FileOutputStreams it out to a temp file, then uses the file. Or, you simply cannot package the pdfs in the jar.

public class PDFWriter {

public static final String testDir = "C:\\pdftest\\"; 
public static final String adobePath = "\"C:\\Program Files\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\"";

public static void main(String[] args){

    try {
        new PDFWriter().run();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void run() throws Exception {
    InputStream in = this.getClass().getResourceAsStream("/resources/test.pdf");
    new File(testDir).mkdirs();
    String pdfFilePath = testDir + "test.pdf";
    FileOutputStream out = new FileOutputStream (pdfFilePath);
    byte[] buffer = new byte[1024];
    int len = in.read(buffer);
    while (len != -1) {
        out.write(buffer, 0, len);
        len = in.read(buffer);
    }
    out.close();

    Runtime rt = Runtime.getRuntime();
    String command = adobePath + " " + pdfFilePath;
    rt.exec(command);
}

}

The only reason the file way works at all in eclipse is because the bin folder is a real folder, but when you build the thing, it all gets zipped up into a jar.

Maybe a bit of confusion over source folders. If you have a source folder called 'src', the package structure under it does not contain "src". src/net/whatever/Class.java will turn into net/whatever/Class.class when build.

So, you could create a second source folder called 'rsrc' (resources), and under this put your resources/order.pdf. rsrc/resources/order.pdf will become resources.order.pdf when you build the jar.

For the sake of easy exporting from Eclipse without constantly thinking about it, I recommend putting the resources folder under the src folder. Long story short, if you don't put it in your src folder, every time you create a jar, you will need to check the box next to the resources folder. So just put it in the src folder and save yourself some problems.

As for referrencing the files, I would try

private String file = "resources/finished.pdf";

This allows you to access the file under src/resources/finished.pdf .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM