Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Create Java Project programmatically(Created project closed after restart)
Create Java Project programmatically [message #1816348] Mon, 28 October 2019 08:57 Go to next message
Miriam H. is currently offline Miriam H.Friend
Messages: 8
Registered: May 2018
Junior Member
Hi,

I´am working on a wizard for creating a Java Project where also Xtext Nature and libraries (xtend and an own library as well) should be added.
Now my Problem: After creating a few projects and restarting the runtime, the project I created at last is closed and I have to open it (right click on project > Open Project). For example I create three projects p1, p2, p3 first, close the runtime then, after a restart p3 would be close.

Here is the program sequence (I summarized it a little for a better overview and removed try catch statements as well):
	
                NullProgressMonitor monitor = new NullProgressMonitor();
		project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		projectLocation = project.getFullPath().toString();
		if (!project.exists()) 
		{
			project.create(monitor);
			project.open(monitor);
		}

// add project natures
		int numberOfNewNatures = 2;
		final String XTEXT_NATURE = "org.eclipse.xtext.ui.shared.xtextNature";
		IProjectDescription desc = project.getDescription();
		String[] prevNatures = desc.getNatureIds(); 
		String[] newNatures = new String[prevNatures.length + numberOfNewNatures];
					
		System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
		newNatures[newNatures.length - 1] = XTEXT_NATURE;
		newNatures[newNatures.length - numberOfNewNatures] = JavaCore.NATURE_ID;
		desc.setNatureIds(newNatures);
		project.setDescription(desc, new NullProgressMonitor());


//now create Java Project
              javaProject = JavaCore.create(project);


//add bin folder (somehow somtimes it already exists and sometimes not...)
		IFolder binFolder = javaProject.getProject().getFolder("bin");
		if (!binFolder.exists())
		{
			binFolder.create(false, true, null);
		}
		javaProject.setOutputLocation(binFolder.getFullPath(), null);
		
		// add JRE System Library to classpath
		IClasspathEntry[] jcp = org.eclipse.jdt.ui.PreferenceConstants.getDefaultJRELibrary();
		setClasspath(jcp);	 
		
//add Xtend Library to classpath
		XtendLibClasspathAdder xLibAdder = new XtendLibClasspathAdder();
		xLibAdder.addLibsToClasspath(javaProject, new NullProgressMonitor());

// add OWN Library
		IClasspathEntry[] ownClasspathEntry = new IClasspathEntry[]{JavaCore.newContainerEntry(OWNClasspathContainerInitializer.OWN_LIBRARY_PATH)};
		setClasspath(ownClasspathEntry);	

//add src folder to class path (ugly workaround to prevent ArrayIndexOutOfBoundsException when src folder already exists)
		IFolder sourceFolder = project.getFolder("src");
		sourceFolder.create(false, true, null);
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder);
		IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
		IClasspathEntry[] newEntries = null;
		IClasspathEntry[] oldEntryWithoutSrc = new IClasspathEntry[oldEntries.length-1];
		boolean srcRemoved = false;
		for (int i = 0; i < oldEntries.length; i++) 
		{
			if (oldEntries[i].getPath().equals(javaProject.getPath()))
			{
				List <IClasspathEntry> entryList = new ArrayList <IClasspathEntry> (Arrays.asList(oldEntries));
				entryList.remove(i);
				entryList.toArray(oldEntryWithoutSrc);
				newEntries = new IClasspathEntry[oldEntries.length];
				System.arraycopy(oldEntryWithoutSrc, 0, newEntries, 0, oldEntryWithoutSrc.length);
				newEntries[oldEntryWithoutSrc.length] = JavaCore.newSourceEntry(root.getPath());
				srcRemoved = true;
				break;
			}
		}
		if (!srcRemoved)
		{
			newEntries = new IClasspathEntry[oldEntries.length + 1];
			System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
			newEntries[oldEntries.length + 1] = JavaCore.newSourceEntry(root.getPath());
		}



		javaProject.setRawClasspath(newEntries, null);
		
		project.refreshLocal(IResource.DEPTH_INFINITE, monitor);


Maybe somone can has a clue what I did wrong? Any help would be appreciated.
Thanks,
Miriam
Re: Create Java Project programmatically [message #1816399 is a reply to message #1816348] Tue, 29 October 2019 05:27 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
I don't see anything obviously wrong. It looks much like this code and I've seen no problems with closed projects:

https://git.eclipse.org/c/emf/org.eclipse.emf.git/tree/plugins/org.eclipse.emf.codegen.ecore/src/org/eclipse/emf/codegen/ecore/Generator.java#n665


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Create Java Project programmatically [message #1816404 is a reply to message #1816399] Tue, 29 October 2019 06:31 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Your summarized sequence may hide the problems. I see no creation of files in particular the MANIFEST.MF. I find it essential to get files in place before opening the project otherwise the concurrent builder starts work with one project description and then needs to rebuild with a changed description. If things change too fast you may confuse Eclipse; incremental building is not perfect. "add bin folder (somehow sometimes it already exists and sometimes not...)" is a clue that the concurrent builder is affected.

In my code I find it prudent to wait and give Eclipse plenty of time to catch up.

	public static void wait(int delayTimeInMilliseconds) {
		for (int i = 0; i < delayTimeInMilliseconds; i += 100) {
			flushEvents();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {}
		}
	}

	public static void flushEvents() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		for (int i = 0; i < 10; i++) {
			while (workbench.getDisplay().readAndDispatch());
		}

	}


Regards

Ed Willink
Re: Create Java Project programmatically [message #1816412 is a reply to message #1816404] Tue, 29 October 2019 08:07 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Obviously you should not sleep on the Eclipse UI thread. My subroutines are for a JUnit Plugin main thread or a worker thread.

Regards

Ed Willink
Re: Create Java Project programmatically [message #1816478 is a reply to message #1816412] Tue, 29 October 2019 22:01 Go to previous message
Miriam H. is currently offline Miriam H.Friend
Messages: 8
Registered: May 2018
Junior Member
Hi,

thank you both for you helpful suggestions!

I finally found a solution for my problem:

I had to put the code above as an IWorkspaceRunnable.run() implementation and now projects stay open. I followed the hint regarding to give eclipse more time as well and it seems that the strange behavior (concerning building) disappeared.

Thank you once again,

Miriam
Previous Topic:Project seemingly lost
Next Topic:What is Advanced Source Lookup for?
Goto Forum:
  


Current Time: Thu Apr 25 09:14:30 GMT 2024

Powered by FUDForum. Page generated in 0.02827 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top