![]() This happens because by default, maven doesn’t bundle the dependency class files along with the application code. 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. $ java -jar target/Įxception in thread "main" : com/github/ricksbrown/cowsay/CowsayĬaused by: : .CowsayĪt java.base/.loadClass(BuiltinClassLoader.java:641)Īt java.base/$AppClassLoader.loadClass(ClassLoaders.java:188)Īt java.base/(ClassLoader.java:520) However, there’s a problem - If we recompile our code and try to run the app now, we will get an error: $ mvn clean compile package ![]() Import static import public class AppTest Package com.sohamkamani // the JUnit library is used for testing Maven can also be used to run tests that we’ve defined in our project.īy convention, all tests reside within the src/test directory.įor the purpose of illustration, let’s create a static method to add two numbers in the App class: Which will give us the output: Hello World! Next, we can execute the JAR file by running: java -jar target/ The clean subcommand removes previous artifacts in the target directory, such as the previous stale JAR file We can now rebuild the project by running: mvn clean package We can add the following configuration as a child of the tag: We can make use of the Maven JAR plugin, which gives us additional capabilities to build JAR files. This is because the JAR file doesn’t know the entry point, so it has no idea where the main method is. If we run this now, we will get the following error: no main manifest attribute, in target/ We can use the java command to execute our JAR file: Advertisements java -jar target/ However, we still have to perform some additional steps before we can run our code. The JAR file is the final output that can be executed by the JVM. Some auxillary files are omitted from here for the sake of clarity Running this command will create a bunch of files in a new target directory: mvn-example Since compile and package are part of the same lifecycle, running mvn package will execute all lifecycle steps upto package, which includes the compile step We can combine these two commands by running mvn package. ![]() We can perform compilation and packaging by running the following command: mvn compile ![]() 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. Compiling our JAR File #īefore running a Java application in production, we’ll need to compile the Java code into byte-code that can be run on the JVM. This generates a new project folder with the following structure: mvn-exampleĪpp.java contains simple code that prints Hello World! when run. I’ve used a groupID corresponding to my domain (), you should replace this with your own choice Advertisements Mvn -B archetype:generate -DgroupId =com.sohamkamani -DartifactId =mvn-example -DarchetypeArtifactId =maven-archetype-quickstart -DarchetypeVersion =1.4
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |