#
Compile & Run a Java Application in Jenkins
This tutorial explains to you how to compile and run a Java application in a Jenkins job.
First of all we need to have a simple Java.
Let's consider the following Java code:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In my case, this code will be put on Jenkins machine in HelloWorld.java
file into /u01/java/app/hello-world
.
In my case also, I run Jenkins on a Linux machine.
pwd
/u01/java/app/hello-world
ls
HelloWorld.java
I could also test the Java application to see if it is working or not.
javac HelloWorld.java
ls
HelloWorld.class HelloWorld.java
java HelloWorld
Hello, World!
Now let's automate this with Jenkins.
For this we need to create a job and to configure it to do the above actions.
So, from the Jenkins console, we need to click on "New item".
We will see something like that:
Enter the item (job) name ("Compile_Run_Java_Application" in my case), choose "Freestyle project" and click on OK button.
You will see something like this:
Go down until you see "Build Steps" section.
Add a build step. In my case I will add "Execute shell", because Jenkins is running under Linux in my case.
After that add the following code into the command section and click on "Save" button.
cd /u01/java/app/hello-world
javac HelloWorld.java
java HelloWorld
At this point the job is defined.
We can go to the console and see the job:
Click on the green triangle, and you will run the job.
Info
Before running the job, you need to run the following command:
chmod -R 777 /u01/java/app/hello-world
or add read rights for the SYSTEM user under which the job is run.
If you go to the Console Output for this Job we can see something like this: