2013-07-02 5 views
1

다음과 같이 code을 클래스 경로의 폴더와 파일에 대해 반복 처리하고 클래스를 결정하고 ID가있는 필드를 가져 와서 logger으로 출력합니다. 내 IDE에서이 코드를 실행하면 잘 작동하지만, 프로젝트를 JAR 파일로 패키지화하고이 JAR 파일을 launch4j로 EXE 파일에 패키징하면 다시 클래스를 반복 할 수 없습니다.직접 JAR 파일의 폴더를 반복합니다.

file:/C:/ENTWICKLUNG/java/workspaces/MyProject/MyProjectTest/MyProjectSNAPSHOT.exe!/com/abc/def 

가 어떻게이 내 JAR/EXE 파일의 모든 내 수업을 반복 얻을 수 있습니다 나는 JAR/EXE 파일에 내 수업을 반복하려고하면 나는 다음과 같은 경로를 얻을?

public class ClassInfoAction extends AbstractAction 
{ 
    /** 
    * Revision/ID of this class from SVN/CVS. 
    */ 
    public static String ID = "@(#) $Id ClassInfoAction.java 43506 2013-06-27 10:23:39Z $"; 

    private ClassLoader classLoader = ClassLoader.getSystemClassLoader(); 
    private ArrayList<String> classIds = new ArrayList<String>(); 
    private ArrayList<String> classes = new ArrayList<String>(); 
    private int countClasses = 0; 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
    countClasses = 0; 
    classIds = new ArrayList<String>(); 
    classes = new ArrayList<String>(); 

    getAllIds(); 

    Iterator<String> it = classIds.iterator(); 

    while (it.hasNext()) 
    { 
     countClasses++; 
     //here I print out the ID 
    } 
    } 

    private void getAllIds() 
    { 
    String tempName; 
    String tempAbsolutePath; 

    try 
    { 
     ArrayList<File> fileList = new ArrayList<File>(); 
     Enumeration<URL> roots = ClassLoader.getSystemResources("com"); //it is a path like com/abc/def I won't do this path public 
     while (roots.hasMoreElements()) 
     { 
     URL temp = roots.nextElement(); 
     fileList.add(new File(temp.getPath())); 
     GlobalVariables.LOGGING_logger.info(temp.getPath()); 
     } 

     for (int i = 0; i < fileList.size(); i++) 
     { 
     for (File file : fileList.get(i).listFiles()) 
     { 
      LinkedList<File> newFileList = null; 
      if (file.isDirectory()) 
      { 
      newFileList = (LinkedList<File>) FileUtils.listFiles(file, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); 

      if (newFileList != null) 
      { 
       for (int j = 0; j < newFileList.size(); j++) 
       { 
       tempName = newFileList.get(j).getName(); 
       tempAbsolutePath = newFileList.get(j).getAbsolutePath(); 
       checkIDAndAdd(tempName, tempAbsolutePath); 
       } 
      } 
      } 
      else 
      { 
      tempName = file.getName(); 
      tempAbsolutePath = file.getAbsolutePath(); 
      checkIDAndAdd(tempName, tempAbsolutePath); 
      } 
     } 
     } 

     getIdsClasses(); 
    } 
    catch (IOException e) 
    { 
    } 
    } 

    private void checkIDAndAdd(String name, String absolutePath) 
    { 
    if (name.endsWith(".class") && !name.matches(".*\\d.*") && !name.contains("$")) 
    { 
     String temp = absolutePath.replace("\\", "."); 
     temp = temp.substring(temp.lastIndexOf(/* Class prefix */)); //here I put in the class prefix 
     classes.add(FilenameUtils.removeExtension(temp)); 
    } 
    } 

    private void getIdsClasses() 
    { 
    for (int i = 0; i < classes.size(); i++) 
    { 
     String className = classes.get(i); 

     Class<?> clazz = null; 
     try 
     { 
     clazz = Class.forName(className); 

     Field idField = clazz.getDeclaredField("ID"); 
     idField.setAccessible(true); 

     classIds.add((String) idField.get(null)); 
     } 
     catch (ClassNotFoundException e1) 
     { 
     } 
     catch (NoSuchFieldException e) 
     { 
     } 
     catch (SecurityException e) 
     { 
     } 
     catch (IllegalArgumentException e) 
     { 
     } 
     catch (IllegalAccessException e) 
     { 
     } 

    } 
    } 
} 
+0

이 항아리를 실행하기 전에 가져온 필수 클래스를 클래스 경로에로드해야합니다. 놓친 적이 없기를 바랍니다. – mitpatoliya

+0

정확히 무슨 뜻인지 모르겠습니까? 나는 무엇을해야합니까? –

+0

자바 7을 사용하면'Filesystems.newFileSystem ("/ path/to/the/jar")처럼 쉽습니다. 'Filesystem' /'Path' 장점을 모두 사용할 수 있습니다. – fge

답변

3

임의의 URL에서 파일 개체를 만들 수 없으며 일반적인 파일 시스템 통과 방법을 사용할 수 없습니다. 지금, 나는 launch4j 어떤 차이가없는 경우 잘 모르겠지만, 일반 JAR 파일의 내용 반복에 관해서는, 당신은 공식 API를 사용할 수 있습니다 조각 위

JarURLConnection connection = (JarURLConnection) url.openConnection(); 
JarFile file = connection.getJarFile(); 
Enumeration<JarEntry> entries = file.entries(); 
while (entries.hasMoreElements()) { 
    JarEntry e = entries.nextElement(); 
    if (e.getName().startsWith("com")) { 
     // ... 
    } 
} 

가 JAR 파일에있는 모든 항목을 나열 url에 의해 참조되는 파일들과 디렉토리들.

관련 문제