129

Is there a way to compile an Eclipse-based Java project from the command line?

I'm trying to automate my build (using FinalBuilder not ant), and I'm neither a Java nor Eclipse expert. I can probably figure out how to do this with straight java command line options, but then the Eclipse project feels like a lot of wasted effort.

In the event that there is no way to compile an Eclipse project via the command line, is there a way to generate the required java command line from within Eclipse? Or are there some files I can poke around to find the compile steps it is doing behind the scenes?


Guys, I'm looking for an answer that does NOT include ant. Let me re-iterate the original question ....... Is there a way to build an Eclipse project from the command line?

I don't think this is an unreasonable question given that I can do something like this for visual studio:

devenv.exe /build "Debug|Any CPU" "C:\Projects\MyProject\source\MyProject.sln"
2
  • 3
    How is there not a plugin / jar for this!!
    – Kieveli
    Commented Sep 15, 2009 at 19:58
  • 1
    Ok... I added a new correct answer.
    – Kieveli
    Commented Sep 16, 2009 at 14:36

9 Answers 9

68

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.

5
  • 4
    Even though I've long since moved on from this project and cannot verify whether or not this answer actually works, I'm changing the accepted answer to this one. Because it indicates that Eclipse does indeed have a command line switch.
    – Keith G
    Commented Sep 30, 2009 at 13:45
  • 3
    Just one thought - seems like this only works for pre-existing workspaces. I guess this makes it hard to, for example, checkout the project from source control and build it this way.
    – John
    Commented Mar 14, 2011 at 13:21
  • I believe my workspace was committed to the svn repository. It either had relative paths, or matching directory structures.
    – Kieveli
    Commented Mar 15, 2011 at 11:14
  • 2
    How do you see the build results? I've tried this, and got output: "Building workspace", but I didn't get indication to whether the build succeeded or failed.
    – Dikla
    Commented Aug 28, 2011 at 10:10
  • You can also build your workspace via a command line ant script. See help.eclipse.org/indigo/… Commented Nov 22, 2011 at 6:32
22

To complete André's answer, an ant solution could be like the one described in Emacs, JDEE, Ant, and the Eclipse Java Compiler, as in:

      <javac
          srcdir="${src}"
          destdir="${build.dir}/classes"> 
        <compilerarg 
           compiler="org.eclipse.jdt.core.JDTCompilerAdapter" 
           line="-warn:+unused -Xemacs"/>
        <classpath refid="compile.classpath" />
      </javac>

The compilerarg element also allows you to pass in additional command line args to the eclipse compiler.

You can find a full ant script example here which would be invoked in a command line with:

java -cp C:/eclipse-SDK-3.4-win32/eclipse/plugins/org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar org.eclipse.core.launcher.Main -data "C:\Documents and Settings\Administrator\workspace" -application org.eclipse.ant.core.antRunner -buildfile build.xml -verbose

BUT all that involves ant, which is not what Keith is after.

For a batch compilation, please refer to Compiling Java code, especially the section "Using the batch compiler"

The batch compiler class is located in the JDT Core plug-in. The name of the class is org.eclipse.jdt.compiler.batch.BatchCompiler. It is packaged into plugins/org.eclipse.jdt.core_3.4.0..jar. Since 3.2, it is also available as a separate download. The name of the file is ecj.jar.
Since 3.3, this jar also contains the support for jsr199 (Compiler API) and the support for jsr269 (Annotation processing). In order to use the annotations processing support, a 1.6 VM is required.

Running the batch compiler From the command line would give

java -jar org.eclipse.jdt.core_3.4.0<qualifier>.jar -classpath rt.jar A.java

or:

java -jar ecj.jar -classpath rt.jar A.java

All java compilation options are detailed in that section as well.

The difference with the Visual Studio command line compilation feature is that Eclipse does not seem to directly read its .project and .classpath in a command-line argument. You have to report all information contained in the .project and .classpath in various command-line options in order to achieve the very same compilation result.

So, then short answer is: "yes, Eclipse kind of does." ;)

9
  • 2
    It is possible to compile a workspace with Eclipse project using the ant4eclipse project, but it needs a lot of elbow grease as you need to do all the necessary steps manually based on the meta information you extract. I've done it for our inhouse projects, but I would not recommend it unless you REALLY need to! We did :-D Commented Jul 6, 2009 at 8:44
  • @Thorbjørn Ravn Andersen: thank you for this feedback. You could also post an answer in this thread illustrating the kind of "elbow grease" involved here ;)
    – VonC
    Commented Jul 6, 2009 at 8:49
  • well, what I hacked together based on ant4eclipse - which requires a projectSet.psf file - to emulate the "export runnable jar" functionality isn't pretty hence no answer. Besides, it is not running Eclipse, but emulating Eclipse using ant :D Commented Jan 21, 2010 at 15:14
  • 1
    Came across this old answer: We found the "emulate Eclipse build using Eclipse configuration files" to be too brittle in the long run and I will strongly advise others not to go that way. Use Maven projects instead. Commented Aug 9, 2014 at 23:53
  • 1
    @JeffPuckettII Thank you. I have restored the link.
    – VonC
    Commented Nov 15, 2016 at 8:04
8

After 27 years, I too, am uncomfortable developing in an IDE. I tried these suggestions (above) - and probably just didn't follow everything right -- so I did a web-search and found what worked for me at 'http://incise.org/android-development-on-the-command-line.html'.

The answer seemed to be a combination of all the answers above (please tell me if I'm wrong and accept my apologies if so).

As mentioned above, eclipse/adt does not create the necessary ant files. In order to compile without eclipse IDE (and without creating ant scripts):

1) Generate build.xml in your top level directory:

android list targets  (to get target id used below)

android update project --target target_id --name project_name  --path top_level_directory

   ** my sample project had a target_id of 1 and a project name of 't1', and 
   I am building from the top level directory of project
   my command line looks like android update project --target 1 --name t1 --path `pwd`

2) Next I compile the project. I was a little confused by the request to not use 'ant'. Hopefully -- requester meant that he didn't want to write any ant scripts. I say this because the next step is to compile the application using ant

 ant target

    this confused me a little bit, because i thought they were talking about the
    android device, but they're not.  It's the mode  (debug/release)
    my command line looks like  ant debug

3) To install the apk onto the device I had to use ant again:

 ant target install

    ** my command line looked like  ant debug install

4) To run the project on my android phone I use adb.

 adb shell 'am start -n your.project.name/.activity'

    ** Again there was some confusion as to what exactly I had to use for project 
    My command line looked like adb shell 'am start -n com.example.t1/.MainActivity'
    I also found that if you type 'adb shell' you get put to a cli shell interface
    where you can do just about anything from there.

3A) A side note: To view the log from device use:

 adb logcat

3B) A second side note: The link mentioned above also includes instructions for building the entire project from the command.

Hopefully, this will help with the question. I know I was really happy to find anything about this topic here.

1
  • 1
    if the project is already built (in debug mode f.ex.) you can use ant installd to install without building
    – serv-inc
    Commented Apr 21, 2015 at 14:53
6

The normal apporoach works the other way around: You create your build based upon maven or ant and then use integrations for your IDE of choice so that you are independent from it, which is esp. important when you try to bring new team members up to speed or use a contious integration server for automated builds. I recommend to use maven and let it do the heavy lifting for you. Create a pom file and generate the eclipse project via mvn eclipse:eclipse. HTH

1
  • 4
    It is frequently much nicer to emulate the current workflow instead of telling every developer to change their behaviour. Especially when Eclipse is much faster than ant. Commented Jan 21, 2010 at 15:16
2

This question contains some useful links on headless builds, but they are mostly geared towards building plugins. I'm not sure how much of it can be applied to pure Java projects.

2

Just wanted to add my two cents to this. I tried doing as @Kieveli suggested for non win32 (repeated below) but it didn't work for me (on CentOS with Eclipse: Luna):

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

On my particular setup on CentOS using Eclipse (Luna) this worked:

$ECLIPSE_HOME/eclipse -nosplash -application org.eclipse.jdt.apt.core.aptBuild  startup.jar -data ~/workspace

The output should look something like this:

Building workspace
Building '/RemoteSystemsTempFiles'
Building '/test'
Invoking 'Java Builder' on '/test'.
Cleaning output folder for test
Build done
Building workspace
Building '/RemoteSystemsTempFiles'
Building '/test'
Invoking 'Java Builder' on '/test'.
Preparing to build test
Cleaning output folder for test
Copying resources to the output folder
Analyzing sources
Compiling test/src/com/company/test/tool
Build done

Not quite sure why it apparently did it twice, but it seems to work.

1

I wanted to just add this comment to this question for anyone who may encounter this issue in the future.

Once, I was working on a project and building complex and inter-related JAVA projects from CMD (using ECLIPSE) was my only option. So, all you need to is this:

  • Download your desired ECLIPSE IDE (tested it with ECLIPSE OXYGEN, Eclipse IDE for Enterprise Java and Web Developers and Eclipse IDE for Java Developers and it works fine)

  • Open your project in the ECLIPSE and change the ECLIPSE configuration based on your project

  • Close the ECLIPSE and save the .metadata created from your ECLIPSE (If your workspace is going to be refreshed in the future)

  • Run this script in the CMD:

"%ECLIPSE_IDE%\eclipsec.exe" -noSplash -data "YOUR_WORKSPACE" -application org.eclipse.jdt.apt.core.aptBuild

So, all I wanted to say is that, I could not build using the above commands without the .metadata being in the workspace. Make sure it exists there. If you need, make a copy of .metadata and copy-paste it each time your want to execute the commands.

0

Hi Just addition to VonC comments. I am using ecj compiler to compile my project. it was throwing expcetion that some of the classes are not found. But the project was bulding fine with javac compiler.

So just I added the classes into the classpath(which we have to pass as argument) and now its working fine... :)

Kulbir Singh

-3

Short answer. No. Eclipse does not have a command line switch like Visual Studio to build a project.

2
  • 6
    Short answer: yes. Eclipse kind of does. Except you have to report all information contained in the .project and .classpath in various command-line options
    – VonC
    Commented Oct 16, 2008 at 4:12
  • External (to Eclipse) build solutions like ant or maven are typically used for this, as noted in other responses. Commented Jul 24, 2009 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.