运行包装在jar中的exe [英] run exe which is packaged inside jar
问题描述
我正在通过我的java程序执行一个exe。
路径在java中是硬编码的。
I am executing an exe through my java program. the path is hardcoded in java.
我已将我的exe打包在jar中。
I have packaged my the exe in the jar.
但我被卡住了,因为我在java文件中硬编码了路径名。
因此无法执行我的jar作为独立程序。
But am stuck as i have the path name hardcoded in the java file. so am not able to execute my jar as a stand alone program.
包装此类jar的任何提示,即内部有exe并且能够
作为独立程序运行吗?
Any hints for packaging such jar i.e having an exe inside and able to run it as a stand alone program?
谢谢,
Krisp
Thanks, Krisp
推荐答案
奇怪的是,今天早上我必须编写类似的代码...感谢我推动我完成这项工作: - )
Oddly enough I have to write similar code this morning... thanks for pushing me to get that done :-)
这将提取 .exe
到本地磁盘上的本地文件。当Java程序存在时,该文件将被删除。
This will extract the .exe
to a local file on the local disk. The file will be deleted when the Java program exists.
编辑:哎呀,忘了实际复制文件......
Whoops, forgot actually copy the file...
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
public class Main
{
public static void main(final String[] args)
throws URISyntaxException,
ZipException,
IOException
{
final URI uri;
final URI exe;
uri = getJarURI();
exe = getFile(uri, "Main.class");
System.out.println(exe);
}
private static URI getJarURI()
throws URISyntaxException
{
final ProtectionDomain domain;
final CodeSource source;
final URL url;
final URI uri;
domain = Main.class.getProtectionDomain();
source = domain.getCodeSource();
url = source.getLocation();
uri = url.toURI();
return (uri);
}
private static URI getFile(final URI where,
final String fileName)
throws ZipException,
IOException
{
final File location;
final URI fileURI;
location = new File(where);
// not in a JAR, just return the path on disk
if(location.isDirectory())
{
fileURI = URI.create(where.toString() + fileName);
}
else
{
final ZipFile zipFile;
zipFile = new ZipFile(location);
try
{
fileURI = extract(zipFile, fileName);
}
finally
{
zipFile.close();
}
}
return (fileURI);
}
private static URI extract(final ZipFile zipFile,
final String fileName)
throws IOException
{
final File tempFile;
final ZipEntry entry;
final InputStream zipStream;
OutputStream fileStream;
tempFile = File.createTempFile(fileName, Long.toString(System.currentTimeMillis()));
tempFile.deleteOnExit();
entry = zipFile.getEntry(fileName);
if(entry == null)
{
throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());
}
zipStream = zipFile.getInputStream(entry);
fileStream = null;
try
{
final byte[] buf;
int i;
fileStream = new FileOutputStream(tempFile);
buf = new byte[1024];
i = 0;
while((i = zipStream.read(buf)) != -1)
{
fileStream.write(buf, 0, i);
}
}
finally
{
close(zipStream);
close(fileStream);
}
return (tempFile.toURI());
}
private static void close(final Closeable stream)
{
if(stream != null)
{
try
{
stream.close();
}
catch(final IOException ex)
{
ex.printStackTrace();
}
}
}
}
这篇关于运行包装在jar中的exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!