XaitormanX
XaitormanX

Reputation: 909

Exe gives error after extracting it from jar

I'm packing a .exe with the other resources on my jar, and the I extract it with the following code:

InputStream program=getClass().getResourceAsStream("/program.exe");
try {
    FileOutputStream output=new FileOutputStream("C:\\Users\\Aitor\\Desktop\\program.exe");
    int b;
    while ((b=program.read())!=1)
    {
        output.write(b);
    }
    output.close();
} catch (IOException e) {
    e.printStackTrace();
}

But i've i try to execute the the exe that is generated, i get an error saying that that archive's version isn't compatible with the windows version I'm using. How can i extract an exe from the jar without corrupting it?

Upvotes: 0

Views: 68

Answers (1)

Gilberto Torrezan
Gilberto Torrezan

Reputation: 5267

I'd use a faster way of reading and writing the file:

FileOutputStream output=new FileOutputStream("C:\\Users\\Aitor\\Desktop\\program.exe");
int b = 0;
byte[] buff = new byte[1024];
while ((b=program.read(buff))>=0)
{
    output.write(buff, 0, b);
}
output.close();

Upvotes: 1

Related Questions