Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Nobu Seay




Posted Questions


No Question(s) posted yet!

Posted Answers



Answer


We will learn how to:

All of these tasks will be done on the command line, so that you can have a better idea on what’s going on under the hood, and how you can run a java application in environments that don’t have a full-featured IDE like Eclipse or IntelliJ.

First, let’s create a new project folder using the maven command line tools:

This generates a new project folder with the following structure:

App.java contains simple code that prints Hello World! when run.

Before running a Java application in production, we’ll need to compile the Java code into byte-code that can be run on the JVM.

If we have multiple classes and folder (which we most likely will), we have to package the compiled code into a common format (like a .jar file).

We can perform compilation and packaging by running the following command:

We can combine these two commands by running mvn package.

Running this command will create a bunch of files in a new target directory:

The JAR file is the final output that can be executed by the JVM. However, we still have to perform some additional steps before we can run our code.

We can use the java command to execute our JAR file:

If we run this now, we will get the following error:

This is because the JAR file doesn’t know the entry point, so it has no idea where the main method is.

We can make use of the Maven JAR plugin, which gives us additional capabilities to build JAR files.

We can add the following configuration as a child of the tag:

We can now rebuild the project by running:

Next, we can execute the JAR file by running:

Which will give us the output:

Maven can also be used to run tests that we’ve defined in our project.

By convention, all tests reside within the src/test directory.

For the purpose of illustration, let’s create a static method to add two numbers in the App class:

We can now create a unit test for this method within src/test/java/com/sohamkamani/AppTest.java:

To run tests, we can run the mvn test command - this will run all tests, tell us how many passed and failed, and give us more information about the failed tests.

Let’s look at how to add dependencies and package them in our JAR file.

For most applications need external libraries (like Spring Boot or Apache Commons) to implement common functionality. Maven allows us to install these dependencies by specifying them in our pom.xml file.

For this example, let’s install the Cowsay library, which will display our output as a quote from a friendly cartoon figure of a cow.

First, we have to add Cowsay as a dependency in our pom.xml file:

Next, we can use the Cowsay.say method within our main method to print the final output string:

However, there’s a problem - If we recompile our code and try to run the app now, we will get an error:

It looks like the Java class loader couldn’t find the classes for the Cowsay library, even though we added it as a dependency in the pom.xml file.

This happens because by default, maven doesn’t bundle the dependency class files along with the application code. To enable this, we can use the maven-assembly-plugin.

This plugin includes all of our applications dependencies into the JAR file. This increases its overall size, but ensures that we can run it as a standalone executable using the java -jar command.

Let’s add the Maven assembly plugin in the pom.xml build definition:

To assemble our JAR file, we can run the assembly:single goal after the compile goal:

This creates a new JAR file in the target directory that you can run using the java -jar command:

You can view the complete example code for this post on Github


Answer is posted for the following question.

Mvn run command?

Answer


A splenectomy is the surgical procedure that partially or completely removes the spleen. The spleen is an important organ in regard to immunological function due to its ability to efficiently destroy encapsulated bacteria.


Answer is posted for the following question.

What is splenectomy?

Answer


The most important factor responsible for the demand for loanable funds is the demand for investment Investment is expenditure of funds on the building up


Answer is posted for the following question.

What factors) affect the demand for loanable funds?


Wait...