您可以编写一个 main 方法来检查文件是否存在,如果不存在则提取它们(通过将资源流复制到文件中)。如果您只需要一组固定的文件(如您的示例中),则可以通过以下方式轻松完成:
public class ExtractAndStart
{
public static void main(String[] args)
{
extractFile("TicTacToe.properties");
Application.main(args);
}
private static void extractFile(String name) throws IOException
{
File target = new File(name);
if (target.exists())
return;
FileOutputStream out = new FileOutputStream(target);
ClassLoader cl = ExtractAndStart.class.getClassLoader();
InputStream in = cl.getResourceAsStream(name);
byte[] buf = new byte[8*1024];
int len;
while((len = in.read(buf)) != -1)
{
out.write(buf,0,len);
}
out.close();
in.close();
}
}
也有自解压档案的创建者,但他们通常提取所有文件,然后启动一些内部组件。
这是一篇简短的文章,它是如何工作的:
http://www.javaworld.com/javatips/jw-javatip120.html
它建议使用似乎已经过时的 ZipAnywhere。但是Ant Installer可能是一个不错的选择。