Java 我无法设置通过代码添加到jar中的类文件的路径
ModifiedFile的路径:D:\ModifiedJavaFiles\com\example\tests\ 因此,这些类是在这个文件夹中生成的,Jar路径是:D:\test.Jar 我想在jar中将这些类的路径设置为com/example/tests 任何帮助都将不胜感激 为了添加信息,在编译类之后,我从路径中获取类并调用updateZipFile方法来更新jarJava 我无法设置通过代码添加到jar中的类文件的路径,java,executable-jar,Java,Executable Jar,ModifiedFile的路径:D:\ModifiedJavaFiles\com\example\tests\ 因此,这些类是在这个文件夹中生成的,Jar路径是:D:\test.Jar 我想在jar中将这些类的路径设置为com/example/tests 任何帮助都将不胜感激 为了添加信息,在编译类之后,我从路径中获取类并调用updateZipFile方法来更新jar public static int compileModifiedClass(String ModifiedFile) {
public static int compileModifiedClass(String ModifiedFile)
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null, ModifiedFile);
System.out.println("Compile result code = " + result);
return result;
}
classes=getClassesFromPath(modifiedfilepath,JarPath);
文件jarFile=新文件(JarPath);
JarUpdater JarUpdater=新的JarUpdater();
尝试
{
updateZipFile(jarFile,类);
}
捕获(IOE异常)
{
e、 printStackTrace();
}
public void updateZipFile(文件zipFile,
文件[]文件)引发IOException{
//获取临时文件
File tempFile=File.createTempFile(zipFile.getName(),null);
//删除它,否则无法将现有zip重命名为它。
tempFile.delete();
布尔renameOk=zipFile.renameTo(tempFile);
如果(!renameOk)
{
抛出新的RuntimeException(“无法将文件“+zipFile.getAbsolutePath()+”重命名为“+tempFile.getAbsolutePath()”);
}
字节[]buf=新字节[1024];
ZipInputStream zin=新的ZipInputStream(新文件输入流(tempFile));
ZipOutputStream out=newzipoutpstream(newfileoutputstream(zipFile));
ZipEntry entry=zin.getNextEntry();
while(条目!=null){
String name=entry.getName();
布尔notInFiles=true;
用于(文件f:文件){
如果(f.getName().equals(name)){
notInFiles=false;
打破
}
}
如果(未填充){
//将ZIP条目添加到输出流。
out.putNextEntry(新ZipEntry(名称));
//将字节从ZIP文件传输到输出文件
内伦;
而((len=zin.read(buf))>0){
out.write(buf,0,len);
}
}
entry=zin.getnextery();
}
//关闭溪流
zin.close();
//压缩文件
对于(int i=0;i0){
out.write(buf,0,len);
}
//完成条目
out.closeEntry();
in.close();
}
//完成ZIP文件
out.close();
tempFile.delete();
}
URL jar=jarFile.toURI().tour();
URL dir=新URL(“文件:/D:/ModifiedJavaFiles/”;
URL[]URL={jar,dir};
URLClassLoader=新的URLClassLoader(URL);
类klazz=loader.loadClass(“com.example.tests.MyTest”);
loader.close();
classes = getClassesFromPath(ModifiedFilesPath, JarPath);
File jarFile = new File(JarPath);
JarUpdater jarUpdater = new JarUpdater();
try
{
jarUpdater.updateZipFile(jarFile, classes);
}
catch (IOException e)
{
e.printStackTrace();
}
public void updateZipFile(File zipFile,
File[] files) throws IOException {
// get a temp file
File tempFile = File.createTempFile(zipFile.getName(), null);
// delete it, otherwise you cannot rename your existing zip to it.
tempFile.delete();
boolean renameOk=zipFile.renameTo(tempFile);
if (!renameOk)
{
throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
}
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
ZipEntry entry = zin.getNextEntry();
while (entry != null) {
String name = entry.getName();
boolean notInFiles = true;
for (File f : files) {
if (f.getName().equals(name)) {
notInFiles = false;
break;
}
}
if (notInFiles) {
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(name));
// Transfer bytes from the ZIP file to the output file
int len;
while ((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
entry = zin.getNextEntry();
}
// Close the streams
zin.close();
// Compress the files
for (int i = 0; i < files.length; i++) {
InputStream in = new FileInputStream(files[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(files[i].getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
tempFile.delete();
}
URL jar = jarFile.toURI().toURL();
URL dir = new URL("file:/D:/ModifiedJavaFiles/");
URL[] urls = { jar, dir };
URLClassLoader loader = new URLClassLoader(urls);
Class<?> klazz = loader.loadClass("com.example.tests.MyTest");
loader.close();