3

Is there a way to create a new Eclipse Java project from a standalone Java application, i.e. create a directory with the files .project, .classpath, src and bin folder?

All solutions I found so far seem to target Eclipse plugins. I'd like to integrate the code into a rather large application that is generating Java source code. So, I'd rather like to stay standalone and independant from Eclipse (using single jar files would be ok, though).

That's about what I've found so far in several samples:


// create Eclipse project
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("SampleProject");
project.create(null);
project.open(null);

// make Java project
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] { JavaCore.NATURE_ID });
project.setDescription(description, null);
IJavaProject javaProject = JavaCore.create(project);

// create bin folder - set output location
IFolder binFolder = project.getFolder("bin");
binFolder.create(false, true, null);
javaProject.setOutputLocation(binFolder.getFullPath(), null);

// add source folders, change classpath,... don't want to be too verbose here

Unfortunately, that code is not suitable for standalone Java applications and would result in an IllegalStateException in the first line saying that the workspace was closed. Is there a better way to solve that problem? In fact, I don't really need any workspace at all, just want to create a single project with .project and .classpath file to ease importing the project for others.

Yes, I've found a lot of similar questions (e.g. Programmatically generate an Eclipse project) - but none of them seems to solve the issue without Eclipse integration.

Community
  • 1
  • 1
user1034081
  • 618
  • 5
  • 21
  • I think there are maven and/or gradle plugins which are able to create eclipse projects. Perhaps, this is another point where to look for code examples. – mschenk74 May 24 '13 at 13:47
  • So do I understand right? You have many Eclipse plugins in your class path including complete Java Development Tools but you don't want to create a workspace folder to stay independent. Indenpent from what? And you call these 50MB of Eclipse classes a standalone Java application. Why? – Bernd Ebertz May 24 '13 at 15:51
  • @meschenk74: yes, seems like I've to check maven source code – user1034081 May 25 '13 at 09:01
  • @Sir RotN: as maven has already been mentioned, that's a nice sample: yes, I personally have Eclipse incl. JDT in my class path. But: Running my application should not require it. Like maven generates Eclipse projects without Eclipse/JDT installed, I'd like to do that. Hope the question is somehow clear but I just don't want to add too much dependencies (so far I'm hoping few jars will do) just for creating trivial .project/.classpath files. – user1034081 May 25 '13 at 09:06
  • 2
    OK, I still don't understand your problem. Do you want to use OSGi bundles of eclipse without instantiating an OSGi container and thus without corretly initializing those bundles? If so, the answer is no. Those plugins often rely on OSGi services and stuff like that. Don't waste your time working around that. Even though this might be possible for each single bit of functionality you will need another pice soon and than start again figuring out how to do it. – Bernd Ebertz May 27 '13 at 11:31
  • Yes, I want to use whatever is necessary to create Eclipse projects. I thought there maybe was some library handling that (and not causing dependency hell). I'm using plain XML files now - as I don't need any fancy features, both the .project and the .classpath files are so simple that I don't want to waste my time finding a more sophisticated solution. – user1034081 May 27 '13 at 11:51
  • I am also looking for a solution for similar kind of problem. Is there any way to create a java project without running the eclipse plugin? I want to create a simple java project in my directory from a java project(not an eclipse plugin project) in my eclipse workspace. Is there any way to do it? – Abhishek Tiwari Aug 06 '16 at 14:27

0 Answers0