Compiling and Running Java Project without IDE

This topic makes no sense to most people since there are too many compelling reasons to use an IDE like Eclipse and IntelliJ for building Java project. Nevertheless, here are a few reasons why this post may interest you.

  1. You are running on a headless OS: Without a GUI, IDEs are out of the question. Then again, almost no one can live solely on a Terminal and such environment is typically found on servers. Who develops project on a server? Just compile everything and deploy onto the server instead!
  2. Curiosity: You want to be a techie or just want to know something about how Java IDEs do their magic(I am one such person).

Anyway, this post is written because a few days back, I was asked how to do so, even though I still cannot comprehend the context nor rationale for doing this. So here’s a quick guide. (Disclaimer: I do not know how things work for developing server-side applications in Java)

Creating the Project
Go to the directory where you want to create the project, create a folder and name it whatever you want(in this case, I use sampleproj). Inside your project folder, create the following folders: srcbin, and lib. As a general guideline, place all .java source files inside src, JAR files of third-party libraries inside lib, and your .class files inside bin. Based on your implementation plan(try to group your classes into packages), create folders inside src to represent the packages that your project is going to have. For example, for package com.example, create in src a folder called com, with a subfolder called example. On Terminal, this can be done by doing a cd into the src folder and then executing mkdir -p com/example. Next, create the source files in their respective locations and start coding. For example, I have a class called com.example.Sample, so I will create Sample.java inside com/example(path is relative to src).

Compiling the Source Codes
Once you are done coding, return to the root directory of your project. The truth is that the magic behind all these is the java command(You need to have installed JDK). java has various options to make all these possible. We will use the -d option to specify where to place the generated class files, and -cp option to specify JAR files from which third-party library classes are imported into your project. Unfortunately, if you have lots of source files and/or JAR files, you cannot use -cp lib/*.jar to specify all JAR files inside lib folder(javac does not recognise * as wildcard character). So you will have to go through the pain of listing ALL your source and JAR files(if you have to build many times due to project iterations or debugging compilation errors)!

Fortunately, there is another way to make life less torturous. java provides a @ annotation to specify file(s) to read options and source files from. Lets create a text file named argyle. Open it with a text editor. On the first line, add -d bin/ as we want all our class files to be in bin directory. On the next line, if you have any libraries to import, add -cp , followed by a list of relative path to each JAR file, separated by ‘:'(for Linux/UNIX, ‘;’ for Windows) . So if you have JAR files A.jar and B.jar, it looks like -cp lib/A.jar:lib/B.jar. Subsequently, list relative paths to all your source files, each on a separate line. Save the file and exit the text editor. Now compile with javac @argfile. It should go without a hitch if there is no compilation error and your class files will be inside bin. For my com.example.Sample class, the class file is in bin/com/example/.

Time to run your project.Every executable project should have 1 point of entry, which is the class with the public static void main(String[] args) method. In my case, it is com.example.Sample class. To execute, run java -cp bin/ , followed by the class to execute. In my case it is java -cp bin/ com.example.Sample. Ta-da! It works(my program only prints “Hello world”)! If you have any runtime errors, it is up to you to fix them.

This wraps up the post. What I feel is that it is too tedious to have to update argfile for every new source file or third-party library added. Thus, I am looking for a way to take this to the next level by automating the update on argyle using a shell script(I’m sure Eclipse does something similar to make developers’ live easy). However, I have yet to find out how to do this as I have not formally learnt shell scripting. This will be left for future posts. Hope this has been a useful and interesting post.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s