Build Eclipse Java Project from Command Line

Build Eclipse Java Project from Command Line

You can build an eclipse project via a workspace from the command line:

eclipsec.exe -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild

It uses the jdt apt plugin to build your workspace automatically. This is also known as a 'Headless Build'. Damn hard to figure out. If you're not using a win32 exe, you can try this:

java -cp startup.jar -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild

Update

Several years ago eclipse replaced startup.jar with the "equinox launcher"

https://wiki.eclipse.org/Equinox_Launcher

On Eclipse Mars (MacOX):

java -jar /Applications/Eclipse.app/Contents/Eclipse/plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar -noSplash -data "workspace" -application org.eclipse.jdt.apt.core.aptBuild

The -data parameter specifies the location of your workspace.

The version number for the equinox launcher will depend on what version of eclipse you have.

Create an Eclipse Java project in command line

Principles of my solution:

  • get templates of .project and .classpath files from existing projects
  • in a Shell script, use and customize this template to create new Project required files
  • in Eclipse, import filesystem folder as a Project (now possible due to created files)

Below is only the relevant part/end of the script, where .project file is created.

Note: I did not need a .classpath file in my case, thus only importing a Project, not Java Project.

# name: the local Eclipse Folder name I give as script arg
# Create Eclipse .project
projectFile="${name}/.project" ;

# below EOF's content comes from an existing real .project file
echo $( cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>${name}</name>
<comment></comment>
<projects></projects>
<buildSpec></buildSpec>
<natures></natures>
</projectDescription>
EOF
) > ${projectFile};
echo "✓ ${projectFile} created" ;

# Create Eclipse .classpath if needed (in case of Java project, not needed for default Project)
...

After this:

  • open Eclipse
  • "File > Import... > Existing Projects into Workspace"
  • select the project folder

Java - Build and run eclipse project from command line

Maven or Ant are the best option but for an Eclipse-only solution

you can choose File -> Export and select Java -> Runnable JAR File
then transfer the JAR file to your other machine and run this from the command line:

java -jar YOUR.JAR

How to build a java project using the command line?

If you are a student willing to have a Java programming career, it might help to learn how to do things from command line, e.g. edit the files, compiling the classes, testing and building the project. Oracle tutorials provide example on this matter: https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html#win32-2

However, I strongly advise you to embrace an IDE as your Java career will mostly reside in an IDE as real life projects are BIG! There are tons of helpful things the IDE does to you out of the box or to simplify things. Since you are a student, I will give you one basic example besides compiling: a class with 10 fields requires you some typing for getters, setters, hashCode, equals. Alternative? Few keystrokes to instruct the IDE to generate them for you. And that's one basic example.

Regarding the project structure, embracing the (since you mentioned it) Maven project structure of src/main/{java,resources}, src/test/{java,resources} even if you do NOT use Maven. This will let you forget about organizing the files around.

If you were asking about structuring the classes in the right packages, you will figure out yourself as you gain experience. Rule of thumb is to group classes together by functionality they provide. Additionally, if the packages are organized right, if you change something and touching a few classes, ideally you'd want the changed classes to be located in a single package if possible.

Learning Maven is a good choice as it is a powerful tool for building a project and keeping things organized (project structure, project dependencies, etc.).

How to compile eclipse project using command line in linux?

This SO Question has an answer that explains how to do it.

I just want to add that it is a bad idea from a couple of respects:

  • If you wire this into your build scripts, you are tying your project to the Eclipse IDE. If someone else who prefers (say) NetBeans has to take over the project, you are making life hard for them. Even more so if this is an open source project.

  • You are potentially make life difficult for yourself too. For instance, suppose that you needed to do an emergency rebuild on a machine that didn't have Eclipse installed, and you couldn't install it for some reason.

  • If you are using Maven (or Ant) in your project, then running the build tool directly from the command line will be faster. And in the case of Maven , running the build tool directly will (in my experience) give you more consistent builds than using embedded Maven.

If you are not familiar with Maven or Ant already, you should learn about them rather than trying to use an IDE as a batch build tool.



Related Topics



Leave a reply



Submit