2014-04-02 21 views
0

我檢出了一些來自SVN的文件,我想用這些文件來生成java項目。然而致命錯誤:1:1:文件提前結束。在線程「org.eclipse.jdt.internal.ui.text.JavaReconciler」中的異常

package uiautomatortestplatform.svn; 
public class SvnProjectImport{ 


    private IProject project; 
    private static ArrayList<String> filesList=new ArrayList<String>(); 
    private String projectName; 
    private String projectPath; 

    public SvnProjectImport(String projectName,String projectPath){ 
     this.projectName=projectName; 
     this.projectPath=projectPath;//該路徑在miuitestmarmot目錄下 
    } 


    public void createMarmotProject() throws FileNotFoundException { 

     IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 
     project = root.getProject(projectName); 
     IPath svnProjectPath=new Path(projectPath); 

     //setting the project attribute 
     NullProgressMonitor monitor=new NullProgressMonitor(); 
     if(!project.exists()){ 
      IProjectDescription description=ResourcesPlugin.getWorkspace().newProjectDescription(project.getName()); 
      description.setLocation(svnProjectPath); 
      String[] oldNatureIds=description.getNatureIds(); 
      String[] newNatureIds=new String[oldNatureIds.length+1]; 
      System.arraycopy(oldNatureIds, 0, newNatureIds, 0, oldNatureIds.length); 
      newNatureIds[oldNatureIds.length]=JavaCore.NATURE_ID; 
      description.setNatureIds(newNatureIds);   
      try{ 
       project.create(description,monitor); 
       project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor,1000)); 
       project.setDescription(description, monitor); 
      }catch(CoreException e){ 
       e.printStackTrace(); 
      } 
     }else{ 
      try{ 
       IProjectDescription description=project.getDescription(); 
       description.setLocation(svnProjectPath); 
       String[] oldNatureIds=description.getNatureIds(); 
       String[] newNatureIds = new String[oldNatureIds.length+1]; 
       System.arraycopy(oldNatureIds, 0, newNatureIds, 0, oldNatureIds.length); 
       newNatureIds[oldNatureIds.length]=JavaCore.NATURE_ID;// 定義工程爲java工程 
       description.setNatureIds(newNatureIds); 
       project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(new NullProgressMonitor(), 1000)); 
       project.setDescription(description, new NullProgressMonitor()); 
      }catch(CoreException e){ 
       e.printStackTrace(); 
      } 
     }  

     //change to java project 
     IJavaProject javaProject=JavaCore.create(project); 

     //get the JRE 
       IClasspathEntry[] jreClasspaths=org.eclipse.jdt.ui.PreferenceConstants.getDefaultJRELibrary(); 

       IClasspathEntry[] oldClasspathEntries=null; 
       try{ 
        oldClasspathEntries=javaProject.getRawClasspath(); 
       }catch(JavaModelException e){ 
        e.printStackTrace(); 
       } 

       Set<IClasspathEntry> newClasspathEntries=new HashSet<IClasspathEntry>(); 
       newClasspathEntries.addAll(Arrays.asList(jreClasspaths)); 
       newClasspathEntries.addAll(Arrays.asList(oldClasspathEntries)); 
       try{ 
        javaProject.setRawClasspath(newClasspathEntries.toArray(new IClasspathEntry[newClasspathEntries.size()]), monitor); 
       }catch(JavaModelException e){ 
        e.printStackTrace(); 
       } 

     //create the folder 
     IFolder binFolder=javaProject.getProject().getFolder("bin"); 
     try{ 
      if(!binFolder.exists()){ 
       binFolder.create(true, true,monitor);    
      } 
      javaProject.setOutputLocation(binFolder.getFullPath(),monitor); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

     IFolder srcFolder=javaProject.getProject().getFolder("src"); 
     try{ 
      if(!srcFolder.exists()){ 
       srcFolder.create(true, true, monitor); 
      } 
      //javaProject.setOutputLocation(srcFolder.getFullPath(), monitor); 
     }catch(CoreException e){ 
      e.printStackTrace(); 
     } 

     //create the files under the src folder 
     String srcPathStr=projectPath+File.separator+"src"; 
     filesList=fileTraversal(srcPathStr); 
     for(String s:filesList){ 
      IPath eachFilePath=new Path(s); 
      javaProject.getProject().getFile(eachFilePath); 
     } 

     IFolder libsFolder=javaProject.getProject().getFolder("libs"); 
     try{ 
      if(!libsFolder.exists()){ 
       libsFolder.create(true, true, monitor); 
      } 
     }catch(CoreException e){ 
      e.printStackTrace(); 
     }  

     //src classpath entry writing to .classpath file 
     IClasspathEntry classpathEntry=JavaCore.newSourceEntry(srcFolder.getFullPath()); 
     newClasspathEntries.add(classpathEntry); 
     IClasspathEntry removeEntry=JavaCore.newSourceEntry(new Path("/"+project.getName())); 
     if(newClasspathEntries.contains(removeEntry)){ 
      newClasspathEntries.remove(removeEntry); 
     } 
     try{ 
      javaProject.setRawClasspath(newClasspathEntries.toArray(new IClasspathEntry[newClasspathEntries.size()]), monitor); 
     }catch(JavaModelException e){ 
      e.printStackTrace(); 
     } 

     String miuiTestMarmotPath=projectPath; 
     String androidJarPath=miuiTestMarmotPath+File.separator+"libs"+File.separator+"android.jar"; 
     String uiautomatorJarPath=miuiTestMarmotPath+File.separator+"libs"+File.separator+"uiautomator.jar"; 
     String miuiMarmotJarPath=miuiTestMarmotPath+File.separator+"libs"+File.separator+"marmot.jar"; 
     InputStream uais=new BufferedInputStream(new FileInputStream(uiautomatorJarPath)); 
     IFile uaFile=javaProject.getProject().getFile("uiautomator.jar"); 
     InputStream is=new BufferedInputStream(new FileInputStream(androidJarPath)); 
     IFile file=javaProject.getProject().getFile("android.jar"); 
     InputStream miuiAutois=new BufferedInputStream(new FileInputStream(miuiMarmotJarPath)); 
     IFile miuiAutoFile=javaProject.getProject().getFile("marmot.jar"); 

     try { 
      file.create(is, false, null); 
      IPath path=file.getFullPath(); 
      newClasspathEntries.add(JavaCore.newLibraryEntry(path, null, null)); 
      uaFile.create(uais, false, null); 
      IPath uaPath=uaFile.getFullPath(); 
      newClasspathEntries.add(JavaCore.newLibraryEntry(uaPath, null, null)); 
      IPath miuiAutoPath=miuiAutoFile.getFullPath(); 
      miuiAutoFile.create(miuiAutois, false, null); 
      newClasspathEntries.add(JavaCore.newLibraryEntry(miuiAutoPath, null, null)); 
      javaProject.setRawClasspath(newClasspathEntries.toArray(new IClasspathEntry[newClasspathEntries.size()]), monitor); 
     } catch (ResourceException e1) { 
      // TODO Auto-generated catch block 
      MessageDialog.openInformation(new Shell(), "Project existed!", "miuiTestMarmot has readly existed!"); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

     try{ 
      javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);//refresh the project 
     }catch(CoreException e){ 
      e.printStackTrace(); 
     }  
    } 

    public static ArrayList<String> fileTraversal(String path){ 
     File srcPath=new File(path); 
     File[] files=srcPath.listFiles(); 
     if(files==null) 
      return null; 
     for(int i=0;i<files.length;i++){ 
      if(files[i].isDirectory()){ 
       fileTraversal(files[i].getAbsolutePath()); 
      }else{ 
       filesList.add(files[i].getAbsolutePath()); 
      } 
     } 
     return filesList; 
    } 
} 

它成功地生成的Java項目,當我雙擊了src文件夾下的.java文件進行編輯的文件,我得到了[致命錯誤]:我的代碼生成如下的Java項目: 1:1:文件提前結束。 線程「org.eclipse.jdt.internal.ui.text.JavaReconciler」錯誤和「java.lang.OutOfMemoryError:PermGen空間」異常我的內存泄漏了嗎?以及如何解決?

+0

只是關於內存泄漏的說明:在你的代碼片段中我沒有看到任何內容關閉'InputStream uais','InputStream is'和'InputStream miuiAutois'(如果你不關閉那些你有資源泄漏,但很可能與你的問題無關的地方) –

+0

這似乎不是Permgen的問題空間 –

+0

是的,但是如果您在泄漏後不關閉它們,它可能會導致問題。 –

回答

0

嘗試增加堆內存大小:

  1. 如果您使用的條命令行

    的java -Xms -Xmx

  2. 運行如果使用eclipse enter image description here

+0

我的eclipse應用程序參數:-Dosgi.requiredJavaVersion = 1.6 -Xms512m -Xmx1024m但它也顯示!ENTRY org.eclipse.ui 4 0 2014-04-02 17:43:06.008 !MESSAGE狀態處理中發生錯誤 !STACK 0 java.lang.OutOfMemoryError:PermGen的空間 異常在線程 「工人-4」 ENTRY org.eclipse.ui 4 0 2014年4月2日17:43:10.917 MESSAGE未處理的事件循環異常 STACK 0! java.lang.OutOfMemoryError:PermGen空間 –

+0

您還可以添加-XX:PermSize = 512m或某些東西,並重試,因爲錯誤java.lang.OutOfMemoryError:PermGen空間異常在線程中是因爲Permgen空間。 –

0

用更多的PermGenSpace運行你的日食。

檢出the eclipse wiki如何設置它。

+0

我已經改變了PermGen空間的大小,但它沒有影響 –

+0

-Dosgi.requiredJavaVersion = 1.6 -Xms512m -Xmx1024m這個錯誤也顯示了當我增加Permgen空間。 –

相關問題