jar - Java Exclude Base Directory from Namespace -
i trying programmatically create runnable jar file. using following code:
the add method:
private static void add(file source, jaroutputstream target) throws ioexception { bufferedinputstream in = null; try { if (source.isdirectory()) { string name = source.getpath().replace("\\", file.separator); if (!name.isempty()) { if (!name.endswith(file.separator)) name += file.separator; jarentry entry = new jarentry(name); entry.settime(source.lastmodified()); target.putnextentry(entry); //target.closeentry(); } (file nestedfile: source.listfiles()) add(nestedfile, target); return; } jarentry entry = new jarentry(source.getpath().replace("\\", "/")); entry.settime(source.lastmodified()); target.putnextentry(entry); in = new bufferedinputstream(new fileinputstream(source)); byte[] buffer = new byte[1024]; while (true) { int count = in.read(buffer); if (count == -1) break; target.write(buffer, 0, count); } target.closeentry(); } { if (in != null) in.close(); } } its implementation:
try { file[] files = new file("tmp").listfiles(); (file file : files) { system.out.println("archiving: "+file.getname()); add(file, target); } } catch (ioexception e) { e.printstacktrace(); } try { target.close(); } catch (ioexception e) { e.printstacktrace(); } i trying add of contents of tmp directory jar, not want prefix of namespaces tmp.. tried using code iterate through the files in tmp , add them keep getting errors saying "no such file or directory". pretty sure that's because looking outside tmp directory. however, when change add(new file("tmp"+file.separator+file.getname()), target); end "tmp" in namespaces (because started tmp/ directory). there way around this?
here example:
i have jar file main-class attribute com.name.proj.projdriver when decompress tmp folder end file in tmp/com/name/proj/projdriver.class. recompress jar using manifest object old jar still specifying main class com.name.proj.projdriver tmp.com.name.proj.projdriver. how can avoid having tmp. prefix namespaces?
replace code this:
private static void add(file source, jaroutputstream target) throws ioexception { bufferedinputstream in = null; try { if (source.isdirectory()) { string name = source.getpath().replace("\\", file.separator); if (!name.isempty()) { if (!name.endswith(file.separator)) name += file.separator; jarentry entry = new jarentry(name); entry.settime(source.lastmodified()); target.putnextentry(entry); //target.closeentry(); } (file nestedfile: source.listfiles()) try{add(nestedfile, target);}catch(ioexception e){system.out.println(e);} return; } jarentry entry = new jarentry(source.getpath().replace("tmp\\","").replace("\\", "/")); entry.settime(source.lastmodified()); target.putnextentry(entry); in = new bufferedinputstream(new fileinputstream(source)); byte[] buffer = new byte[1024]; while (true) { int count = in.read(buffer); if (count == -1) break; target.write(buffer, 0, count); } target.closeentry(); } { if (in != null) in.close(); } } i have varied row:
jarentry entry = new jarentry(source.getpath().replace("tmp\\","").replace("\\", "/"));
Comments
Post a Comment