Given a webstart application composed of multiple jars, how could I list the files contained in these jars? (at runtime)
Thanks in advance,
A开发者_如何学JAVArnaud
EDIT:
The problem, with the method mentionned below (which is very similar to what I used until now), is that somehow the classpath changes when invoking webstart. Indeed, it doesn't reference your jars anymore but a deploy.jar
instead.
As a consequence, if you run java -cp myjars test.ListMyEntries
it will correctly print the content of your jars. On the other hand, via webstart, you will obtain the content of deploy.jar
because this is how the classpath is defined when webstarted. I did not found any trace of the original jar names in any of the system/deployment properties.
Output sample:
Entries of jar file /usr/lib/jvm/java-6-sun-1.6.0.06/jre/lib/deploy.jar
META-INF/
META-INF/MANIFEST.MF
com/sun/deploy/
com/sun/deploy/ClientContainer.class
com/sun/deploy/util/
com/sun/deploy/util/Trace$TraceMsgQueueChecker.class
Yes you can.. But you should sign the jar that class resides and give all permissions..
static void displayJarFilesEntries(){
String cp = System.getProperty("java.class.path");
String pathSep = File.pathSeperator;
String[] jarOrDirectories = cp.split(pathSep);
for(String fileName : jarOrDirectories){
File file = new File(fileName);
if(file.isFile()){
JarFile jarFile;
try{
jarFile = new JarFile(fileName);
} catch(final IOException e){
throw new RuntimeException(e);
}
System.out.println(" Entries of jar file " + jarFile.getName());
for(final Enumeration<JarEntry> enumJar = jarFile.entries(); enumJar
.hasMoreElements();){
JarEntry entry = enumJar.nextElement();
System.out.println(entry.getName());
}
}
}
}
If we have disk access to these jars (which i think we would have) then jdk's JarFile class can be used to open up and iterate over the contents of a jar file. Each entry contained in Enumeration returned by entries method is a class name within the jar.
Have you tried using
System.getProperty("java.class.path");
Alternately, you can use JMX :
RuntimeMXBean bean = /* This one is provided as default in Java 6 */;
bean.getClassPath();
This is how I did it:
public static List<String> listResourceFiles(ProtectionDomain protectionDomain, String endsWith) throws IOException
{
List<String> resources = new ArrayList<>();
URL jar = protectionDomain.getCodeSource().getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
while(true)
{
ZipEntry e = zip.getNextEntry();
if(e == null) break;
String name = e.getName();
if(name.endsWith(endsWith)) resources.add(name);
}
return resources;
}
List<String> workflowFilePaths = AppUtils.listResourceFiles(getClass().getProtectionDomain(), ".bpmn20.xml");
精彩评论