Build Eclipse Java Project from Command Line - java

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"

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.

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." ;)

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.

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

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.

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.

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.

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

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

Related

Build Repast Models via CLI

I am currently looking for a way to build Repast models via CLI and then start the model with the batch-runner. Later the process is supposed to be automatically executed within a CI/CD pipeline. Therefore the building process should be executed independent from Eclipse.
I have already tried the Java compiler and the Groovy compiler which had issues locating the Repast specific classes. Currently, it only works via CLI with the Equinox launcher within Eclipse and only when the model is in an Eclipse workspace. However, the model needs to be built independently of Eclipse.
Does anyone perhaps have an idea about the problem?
This appears to be possible with pure Java Repast Simphony projects, e.g., JZombies. You will need to specify a workspace where the project exists and issue the following:
<path/to/eclipse/>eclipse -nosplash -application org.eclipse.jdt.apt.core.aptBuild -data "<path/to/workspace>"
We're looking into how to successfully do this with Repast Simphony projects that invoke additional builders (e.g., Statechart, ReLogo).
Eclipse has the option to export an Ant build.xml file without the Eclipse compiler from the Project. You still need a subset of the JARs and Libs in the Eclipse plugins folder, but this allows to build and even execute the GUI without having Eclipse installed.

Can I get the javac command equivalent to an Eclipse export to JAR for a project (or NetBeans)?

I have the following task at hand: I have a Java project in Eclipse on machineA that I need to run on machineB. These machines are similar but different and I am getting an error trying to run on B the JAR that I exported on A. So what I want to do it to try rebuilding my code on B. Now, a standard way to do that would be to set up the project on A as an Ant or Maven build, copy the project to B and then run Ant/Maven on B from command line (I don't have Eclipse installed on B). However, B is a bit... "special" in different ways. For instance, its default java is 1.6 but it also has a local install of 1.7 that I am using. That install has both java and javac. There is no Maven there at all and there is a custom Ant install that may not correspond to the java install.
So the question is: is there a way to generate the javac command that would be used on A to export to JAR, so that I could run that command from the command line, possibly with some mods, to (re)build the project on B?
UPDATE: Would it be easier to do the same in Netbeans? If so, I would be willing to give it a try.
I think what you want is the jar command, as opposed to a javac option. The jar command, part of the Java JDK, creates a jar file for you. There is documentation here.
But this will create the same jar that eclipse will create, so I expect there is some problem running it on your B machine that does not have to do, strictly, with creating jar files. So more detail on the problem you're having on B would help.

Ant build with Eclipse Mars fails due to missing classpath reference

I recently moved to Eclipse Mars and installed JDK 8u51. I'm using the JBoss Tools plugin with my Eclipse and have a couple of Ant files to build my projects.
When I double-click on a build file in the Ant view, however, my project won't built. Instead I get this error message:
The archive: C:/Program%20Files%20(x86)/Eclipse%20Mars/plugins/org.eclipse.swt.win32.win32.x86_64_3.104.0.v20150528-0211.jar which is referenced by the classpath, does not exist.
I checked the file system and the JAR is actually there. Any ideas how I could fix that? Maybe it's the spaces (%20) in the path name?
Unfortunately, I don't know where this dependency comes from. In the External Tools Configuration it is under Additional Tasks & Support, but I cannot edit the entry there. Nor did I find any other dialog where I can actually configure this reference.
Having had the exact same issue with Eclipse Mars, it turned out the problem was related to the '%20' stuff and not to the jar in itself.
You can either work on a different folder than "Program Files" (install eclipse somewhere else, ...), or create a symbolic link so that ant can see the existing repository the way it knows it. Run the following command in the elevated command prompt(Start > Type cmd > Right click and Run as administrator)
mklink /d c:\Program%20Files%20(x86) "c:\Program Files (x86)"
(and the same for "Eclipse Mars" directory, that you could also rename to remove the space)
Only need to remove blank spaces in the directory name.
Eclipse 4.5.1 was released on October the 2nd and Bug 470390 has been fixed.
The update resolves this issue.

How to run ant script where ant is not installed?

I have an ant script which compiles java code and executes it but when I run it on a machine where ant is not installed it does not execute nor compile. Is there any way to do this?
Thanks
No, there is no way to do this.
Basically, running a program require having this program installed. No mater what program it is.
Copy bin and lib directories from a Ant -package to your project path and run "bin\ant".
Or for example you project path could contain:
build.xml
src
software\ant\bin
software\ant\lib
You can run "software\ant\bin\ant" in you project path.
You can try to create something similar to the gradle wrapper for ant.
Basically, it's a batch/shell script and a simple jar that only need a valid JAVA_HOME to run. When you launch this script it download gradle if required (i.e. not already available at a well known location), then it execute your gradle build.
Imagine someone saying, "I have a C/Python/Perl/C++/Whatever program I want to compile. Is there anyway to compile that C/Python/Perl/C++/Whatever program without having to install C/Python/Perl/C++/Whatever?"
The answer would pretty much be no. You need Ant to interpret the Ant build files.
Fortunately, installing Ant is pretty straight forward. You download the ZIP file from Ant's Distribution Page, then unzip it into some directory (preferably one without spaces in the name. C:\Program Files\Apache Ant isn't a good choice, but C:\apache-ant would be fine.
Now, you set two environment variables, ANT_HOME pointing to where you installed Ant, and JAVA_HOME pointing where you installed the Java JDK. (Windows comes with the Javaruntime, but you need to have the Java JDK which you can get from Oracle.) This can be done by going into the System Control Panel->Advanced
WARNING: When installing Java Developer Kit (JDK), be very, very careful not to accidentally install the Ask Toolbar. Java does this by default.
Once that is done, set your PATH (again via the PATH Environment variable to include %ANT_HOME/bin and %JAVA_HOME%/bin at the beginning of your path.
Then, running ant on the command line in a Console window will run Ant. The whole procedure takes about 10 minutes to do.

Mac OS X: Setting up Xcode to use Ant results in permission errors

I'm trying to setup Xcode as my primary IDE to work with our existing projects but running into some permissions problems.
Our projects are java based and we use ant to build the jar and it all runs under Tomcat.
Up until now, I have been working strictly from the terminal and a text editor. This has been working fine, but I'm trying to get the Xcode IDE integrated to streamline the development process a bit.
I have my deploy scripts that I wrote and work great, so there's no problem in my existing process(other than that its a little cumbersome).
I created a new empty project in Xcode and created a target that runs the ant build command and make file for one of our projects. I usually run the build command as:
sudo ant -q -f /pathto/build.xml clean dist-debug
This cleans for each build and helps to prevent odd problems. The sudo part is what's catching me right now.
When I run the build in Xcode, it start but fails when trying to do the clean. I get file permissions problems. I have gone in and set the permissions to 777 on the directory and its contents and changed the group to my user account but it still fails the same way.
Any recommendations on how to set this up?
Found the answer. I was getting an identical permission error when trying to use the ant dist-debug in the terminal in the project directory.
Because my build scripts were using sudo there were remnants after each build that could only be cleaned out by another sudo rm command. After I went through and manually deleted the cruft from the project directories, I was able to run the command successfully in terminal. Then I ran it using Xcode and it worked perfectly.

Categories