我有一个程序需要能够创建自己的可执行JAR文件,但遗憾的是我在使其工作时遇到了一些麻烦。这是我目前用来创建JAR的方法:
public static void createJar() throws IOException {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "JarTest");
JarOutputStream jos = null;
try {
String jarPath = System.getProperty("user.dir") + "/Test.jar";
jos = new JarOutputStream(new FileOutputStream(jarPath), manifest);
}
catch (IOException e) {e.printStackTrace();}
ArrayList<String> fileList = new ArrayList<String>();
String codeDir = System.getProperty("user.dir") + "/bin/jartest/";
Files.list(Paths.get(codeDir)).forEach(entry -> {
fileList.add(((Path)entry).toString());
});
int len = 0;
byte[] buffer = new byte[1024];
for(String file : fileList ) {
//create JarEntry
JarEntry je = new JarEntry(file);
je.setComment("Creating Jar");
je.setTime(Calendar.getInstance().getTimeInMillis());
System.out.println(je);
jos.putNextEntry(je);
//write the bytes of file into jar
InputStream is = new BufferedInputStream(new FileInputStream(file));
while((len = is.read(buffer, 0, buffer.length)) != -1)
jos.write(buffer, 0, len);
is.close();
jos.closeEntry();
System.out.println("Done");
}
jos.close();
}
执行此操作时,不会发生错误,我会在Eclipse项目文件夹中生成一个名为“Test.jar”的JAR文件。但是当我打开JAR时,.class文件不存在,而是我看到了:
我知道它正在找到.class文件,因为行System.out.println(je);
成功打印出每个文件的绝对路径,那么为什么不将它们放入JAR中呢?
答案 0 :(得分:0)
当我改变这两行时它解决了自己:
meow://s4mpl3.s4mpl3.top/?l=&k=2OuX2&p=ESexoc&m=&i=cZzwP0gj_&j=kssjA2k&t=yNQx2&s=&a=HruxaXM0&e=ka5DwfIx
对于这些:
String jarPath = System.getProperty("user.dir") + "/Test.jar";
String codeDir = System.getProperty("user.dir") + "/bin/jartest/";
我现在可以在JAR中看到.class文件了。