Skip to main content
blog title image

3 minute read - Java For Testers Maven

What is a Java `main` method - simple example

Mar 22, 2016

One of the topics I don’t cover in “Java For Testers” is the main method. I explain why my coding style ‘as a tester’ doesn’t really require main methods in this blog post.

And in this blog post I’m going to start to explain the main method.

Why?

To round off our Java education a little so that if you do want to start writing small applications or package your well written Java library code into an app, then you know how to go about doing it.

Create a Project

My basic steps, and I’m not going to cover them in detail because they are covered in Java For Testers:

  • creating a maven project in intellij,
  • but I’m not going to add any dependencies into the pom file.

Create a class with main method

And I will create the standard “Hello World” application:

  • create a class
  • create a public static void method called main which takes a String array as arguments
  • for the body of the method I will println the String "Hello World!" to the standard out
package com.javafortesters.main;

public class HelloWorldOutputter {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Because we have written a lot of @Test methods in the IDE, we know that we can right click on a method and run it as a JUnit Test.

We can do the same to the main method we have just written and right click it to run it.

And we should see "Hello World!"printed to the console.

Great, so we’ve written our first app then?

Actually, no. We have written a class with a main method.

Create a .jar file

We have to first of all create a .jar file.

Since we used Maven we can do that very simply from the command line by typing mvn package

And then in our target folder we will see the ‘application’ as a .jar file.

Woo hoo. So now we have written our first app?

Actually no. Try running it

D:\>java -jar mainMethodInvestigation-1.0-SNAPSHOT.jar

no main manifest attribute, in mainMethodInvestigation-1.0-SNAPSHOT.jar

I need a manifest attribute?

Yes.

But we could run it now without a manifest attribute.

Java allows us to run any main class in the classpath from the command line, it just makes things a little more complicated for the average user. You can see examples of me doing this in my “Technical Testing Case Study

The manifest attribute makes it easier.

D:\>java -cp mainMethodInvestigation-1.0-SNAPSHOT.jar com.javafortesters.main.HelloWorldOutputter
Hello World!

What we’ve basically said here is…

Java, add mainMethodInvestigation-1.0-SNAPSHOT.jar to the classpatth and run the main method that you find in class com.javafortesters.main.HelloWorldOutputter and yes, you need the full package.

Woo hoo?

But I really want a manifest attribute

OK, that is easy to add into maven.

At its most basic, and for this current ‘application’ it can be pretty basic. We just need to use the maven-jar-plugin and configure the mainClass. documentation

To do that, we add the following into the pom.xml

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <!-- http://maven.apache.org/shared/maven-archiver/index.html#class_manifest -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.javafortesters.main.HelloWorldOutputter</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

And now, when I run mvn package.

And then when I run the .jar

D:\>java -jar mainMethodInvestigation-1.0-SNAPSHOT.jar
Hello World!

So is that an application?

Yes it is.

Woo hoo!

There is some information on the Oracle site: