Java从命名空间中排除基本目录
我试图以编程方式创建一个可运行的jar文件。我正在使用以下代码: 添加方法:Java从命名空间中排除基本目录,java,jar,namespaces,archive,executable-jar,Java,Jar,Namespaces,Archive,Executable Jar,我试图以编程方式创建一个可运行的jar文件。我正在使用以下代码: 添加方法: private static void add(File source, JarOutputStream target) throws IOException { BufferedInputStream in = null; try { if (source.isDirectory()) { String name = so
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();
}
for (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();
}
finally
{
if (in != null)
in.close();
}
}
其执行:
try {
File[] files = new File("tmp").listFiles();
for (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();
}
我试图将tmp目录的所有内容添加到我的jar中,但我不想在所有名称空间前面加上tmp.
。我尝试使用这段代码在tmp中迭代文件并添加它们,但我不断收到错误,说“没有这样的文件或目录”。我很确定这是因为它正在tmp目录之外查找。但是,当我将其更改为add(新文件(“tmp”+File.separator+File.getName()),target)代码>我在名称空间中以“tmp”结束(因为我从tmp/目录开始)。有办法解决这个问题吗
以下是一个例子:
我有一个jar文件,其属性为com.name.proj.ProjDriver
当我将其解压缩到tmp文件夹中时,我最终得到了tmp/com/name/proj/ProjDriver.class
中的文件。然后,我使用旧jar中的manifest对象重新压缩jar,它仍然将主类指定为com.name.proj.ProjDriver
,但现在它实际上是tmp.com.name.proj.ProjDriver
。如何避免将tmp.
作为所有名称空间的前缀?用以下代码替换您的代码:
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();
}
for (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();
}
finally
{
if (in != null)
in.close();
}
}
我改变了这一行:
JarEntry entry = new JarEntry(source.getPath().replace("tmp\\","").replace("\\", "/"));
问题不是我找不到文件。我可以通过修改代码来做到这一点,正如我在回答中所示。问题是,我所有的名称空间都是从tmp开始的。
@735Tesla:基本上,您只需要这些更改中的第一个-到条目。我刚刚测试了它,它很好。正如Jon所说的那样进行了更正。这将不起作用,因为每个类顶部的包语句将是错误的。另外,其他课程将无法引用它们。请原谅。我尝试了另一个答案,在我的电脑里它可以工作,希望它能帮助你到达你想去的地方。