Question

I have an exe packaged inside my jar file and I am trying to copy it to a temporary location so that i can run it using Desktop.browse(), to do this I set up a scanner with the input stream constructor using class.getResourceAsStream, and then with a printwriter wrote that all to a file. The problem that occurred says that the exe is invalid. I think this is due to some binary data being lost. If anyone can help please post a comment.

    Scanner sc = new Scanner(ClassBuilder.class.getResourceAsStream("jd-gui.exe"));
    File copy = new File("C://users//Owner//Desktop//java//jd-gui.exe");
    copy.createNewFile();
    PrintWriter writer = new PrintWriter(copy);

    while(sc.hasNextLine())
        writer.println(sc.nextLine());

    writer.flush();
    writer.close();
    sc.close();

    Desktop.getDesktop().browse(copy.toURI()); 
Was it helpful?

Solution

As already mentioned use streams for binary data. Commons io makes copying streams easy. Something like:

InputStream in = getClass().getResourceAsStream("jd-gui.exe");
OutputStream out = new FileOutputStream("jd-gui.exe");
IOUtils.copy(in, out);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top