JAR File Exception Exception

I would like to know how to create a runnable JAR with resources (images, pdf) in a resource folder inside or outside the source package (e.g. / src / resources / images / or / resources / images /) in Eclipse.Currently I have my resources are inside the source folder of my eclipse project, but I also tried it in my own folder in the package. The program builds and runs fine in eclipse, but when I go to export as a running jar, I keep getting a file not found exception when I run it on the desktop. I am declaring my files with a line like this

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

      

I understand that the user should getResourceAsStream (), but due to some restrictions I cannot (has to do with how files are saved, I read whole pdf files, not as streams), so I am wondering how to get my files into the right place in the bank. If I unzip it after I've done it, they always appear at the top level, outside of the folders. Here is a screenshot of my current project structure. To say this, this project works fine in eclipse, also in java build properties, source folder is in build path along with all sub folders, I also tried to do the same with empty resource folder in earlier test. enter image description here

+3


source to share


2 answers


You will need to do getResourceAsStream and make sure the pdfs are embedded in the jar. Since you have them in your source folder, they should.

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

If you have to have an honest file, then you need code that reads the PDF as a resource stream, FileOutputStreams it in a temporary file, and then uses the file. Or you just can't jar the PDF files.

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 path works at all in eclipse is because the bin folder is a real folder, but when you create it, it all gets stitched into a jar.

Maybe confusion about source folders. If you have a source folder named "src", the package structure below it does not contain "src". src / net / whatever / Class.java will turn to net / whatever / Class.class on build.

So, you can create a second source folder called "rsrc" (resources) and in doing so enter your /order.pdf resources. rsrc / resources / order.pdf will become resource.order.pdf when you create the jar.

+6


source


For easy export from Eclipse without thinking about it, I recommend putting your resources folder in a folder src

. In short, if you don't put it in your folder src

, every time you create a jar, you will need to check the box next to the folder resources

. So just put it in a folder src

and save some problems.

As for linking files, I would try



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

      

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

.

+1


source







All Articles