# Excuting code

* [Excuting code](/workspace/languages/java/beginner/executions.md#excuting-code)
  * [Have main method](/workspace/languages/java/beginner/executions.md#have-main-method)
  * [Use command line for single class](/workspace/languages/java/beginner/executions.md#use-command-line-for-single-class)
  * [Use intellij](/workspace/languages/java/beginner/executions.md#use-intellij)
  * [Use jar](/workspace/languages/java/beginner/executions.md#use-jar)
  * [Manifest file](/workspace/languages/java/beginner/executions.md#manifest-file)
    * [using maven](/workspace/languages/java/beginner/executions.md#using-maven)
      * [Build jar](/workspace/languages/java/beginner/executions.md#build-jar)
      * [Run jar](/workspace/languages/java/beginner/executions.md#run-jar)
    * [Command line](/workspace/languages/java/beginner/executions.md#command-line)
      * [creating jar](/workspace/languages/java/beginner/executions.md#creating-jar)
      * [Running jar](/workspace/languages/java/beginner/executions.md#running-jar)
      * [Running main() method](/workspace/languages/java/beginner/executions.md#running-main-method)
  * [Use Docker](/workspace/languages/java/beginner/executions.md#use-docker)
    * [Using maven fabric8](/workspace/languages/java/beginner/executions.md#using-maven-fabric8)

## Have main method

To run a Java app a main() method must be supplied. This is where all the code is wired up (objects newed up) and methods run. It is the entry point of a Java app.

## Use command line for single class

This way is best for single file apps (which is rare). If using an IDE then these steps are very easy and wont really ever need to use the command line. Must have a main method in the class.

NOTE: make sure the path for the jdk is setup

1. compile the class

   ```bash
   javac Helloworld.class
   ```
2. Run the compiled class

   ```bash
   java Helloworld
   ```

   * running with arguments that are passed to the main(), all args serparted by space

     ```bash
     java Helloworld arg1 arg2 arg3
     ```

## Use intellij

In class with main, click on the green arrow next to the main() method on the left hand side.

## Use jar

* <https://docs.oracle.com/javase/tutorial/deployment/jar/index.html>
* <https://en.wikipedia.org/wiki/JAR_(file_format>)

## Manifest file

A manifest file is created when a jar is created. It contains info about the files that packaged in the jar

* Add entry point for running jar

In manifest file, add `Main-Class: <MyPackage.MyClass>`. Then run the jar.

NOTE: Always have empty line or carriage return on last line

* <https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html>

### using maven

Always good to have

#### Build jar

* Simple jar
  1. Add to pom.xml

     ```markup
     <build>
      <plugins>
        ...
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <configuration>
              <!-- DO NOT include log4j.properties file in your Jar -->
              <excludes>
                <exclude>**/log4j.properties</exclude>
              </excludes>
              <archive>
                <manifest>
                  <!-- Jar file entry point, where main method is-->
                    <mainClass>com.hanfak.wiring.App</mainClass>
                </manifest>
              </archive>
          </configuration>
        </plugin>
     ```
  2. Run `mvn package`
* Uber/Fat jar - Have jar with dependencies
  1. Add to pom.xml

     ```markup
     <build>
      <plugins>
        ...
      <!-- Make this jar executable -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>**/log4j.properties</exclude>
          </excludes>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.hanfak.wiring.App</mainClass>
              <classpathPrefix>dependency-jars/</classpathPrefix>
            </manifest>
          </archive>
        </configuration>
      </plugin>

      <!-- Copy project dependency -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
          <execution>
          <id>copy-dependencies</id>
          <phase>package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <!-- exclude junit, we need runtime dependency only -->
            <includeScope>runtime</includeScope>
            <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
          </configuration>
          </execution>
        </executions>
      </plugin>
     ```
  2. Run `mvn package`

Or use maven-assembly-plugin

```markup
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>attached</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <finalName>App</finalName>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </execution>
    </executions>
  </plugin>
```

Can also use **shade plugin** <https://maven.apache.org/plugins/maven-shade-plugin/index.html>

* <https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/>
* <https://stackoverflow.com/questions/11947037/what-is-an-uber-jar>

#### Run jar

1. Add to pom.xml

   ```markup
   <build>
      <plugins>
        ...
          <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>exec-maven-plugin</artifactId>
              <version>1.2.1</version>
              <configuration>
                  <mainClass>com.example.Main</mainClass>
              </configuration>
          </plugin>
        ...
      </plugins>
   </build>
   ```
2. In command line run: `mvn exec:java`

NOTE: The phase this is set is not specified, so should be run as pluging goal instead of using lifecylce ie mvn verify

### Command line

#### creating jar

* Add individually:

  `jar cvf <name of jar>.jar file1 file2 directory1 directoy2 ...`
* Add all, must be in directory

  `jar cvf <name of jar>.jar .`

NOTE: `v` in the flag is for verbose, and is optional

#### Running jar

`java -jar helloworld.jar`

#### Running main() method

`java -cp jarName.jar <packageName.ClassName> <argumentsIfAny>`

## Use Docker

1. Create the jar as above, and check it working
2. Create a dockerfile
3. Run container

see project here <https://github.com/hanfak/docker-java-maven-rapidoid> this has notes on how to set it up. I have used the maven-assembly-plugin to create the jar. Where the docker file runs this jar.

### Using maven fabric8


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hanfak.gitbook.io/workspace/languages/java/beginner/executions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
