我檢出了一些來自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空間」異常我的內存泄漏了嗎?以及如何解決?
只是關於內存泄漏的說明:在你的代碼片段中我沒有看到任何內容關閉'InputStream uais','InputStream is'和'InputStream miuiAutois'(如果你不關閉那些你有資源泄漏,但很可能與你的問題無關的地方) –
這似乎不是Permgen的問題空間 –
是的,但是如果您在泄漏後不關閉它們,它可能會導致問題。 –