Build and run eclipse java projects from the command line

You can skip the context of this post if all you need is the commands.

Some context

Recently, I had to work on a Java assignment. I created a Java project in Eclipse and jumped right into the assignment.

This was great as I got started really quickly, writing tests, running them, debugging my code, stepping through them in the debugger, fixing compile time errors etc. Eclipse handled all these things without much setup on my part.

However, for my assignment, the final submission required me build and run my java program from the command line. This shouldn't be a big problem (I thought) and it wasn't, the only unfortunate things was that googling around for a solution took longer than I expected.

I had expected StackOverflow to already contain the answer to my question since I figured someone probably faced the same issue before.

I found the following:

All of which were not particularly helpful for my situation..

To be clear, I wanted

  • Simple javac and java commands that I could run (or add to a Makefile) such that the .class files generated would go into the bin folder created in the eclipse project. (So running it via the command line would be similar to running it in eclipse in terms of what files were generated.)
  • Possibly a way to run my junit tests from the command line too. (less important).

My project setup.

My project structure is as follows:

cs5223
  |- .classpath
  |- .project
  |- src
      |- cs5223
          |- Server.java
          |- Player.java
          |- Game.java
  |- bin
  |- test
      |- cs5223
          |- PlayerTest.java
          |- GameTest.java

Building the project

Run the following command to compile your .java file in src and put them in bin

javac -d bin/ -cp src /path/to/java/file

My package was called cs5223 and I was trying to build cs5223.Server.java (which was the entry point into my project).

javac -d bin/ -cp src src/cs5223/Server.java

Running the project

Once you've compiled your .java files, you run them using the following command.

java -cp bin JAVA_CLASS

My Server class had the following fully qualified package path: cs5223.Server so I ran the following command.

java -cp bin cs5223.Server

Running my junit tests

This required downloading a junit jar which I placed in my root folder. (Pretty sure you can avoid doing this, but this was cleaner imo)

# Building the tests.
javac -cp junit-4.8.1.jar:src -d test/ \
    test/cs5223/GameTest.javacva \
    test/cs5223/PlayerTest.java

# Runing the tests
java -cp test:bin:junit-4.8.1.jar orrg.junit.runner.JUnitCore \
    cs5223.GameTest \
    cs5223.PlayerTest

Semicolons are used to demarcate multiple paths.