Main class execution maven or java command - java

I have a simple java main class and can execute it using the below command on server
java com.....MainClass
Now we are trying to use Maven in our application.
I am seeing on web that the maven command to execute java program is
mvn exec:java -Dexec.mainClass=com......MainClass
Can I still use regular java command or do I have to use the maven command exec:java?
It's not clear to me when to use the maven exec:java command.
Please help me understand when to use regular java command and when to use the maven exec:java command.
when I use Maven can I still execute program using regular java command.

Maven
According to https://maven.apache.org/what-is-maven.html Maven's objectives are
Making the build process easy
Providing a uniform build system
Providing quality project information
Providing guidelines for best practices development
Allowing transparent migration to new features
So, you should use Maven for building your project... like building a JAR package with main class.
Running JAR packaged main class
See this answer or this Oracle's tutorial for running a JAR packaged Java class.
You can also use Maven for running the main class but you should not make your production environment depended on Maven.

maven is mainly a dependency mangement and a build tool.
u can still use java command to run your main class from the jar generated by maven under the target folder of your project.
java -cp "yourProject/target/folder-containing-dependenc-jars-if-exist/*:yourProject/target/thejar.jar" package.mainClass
ps: use the ":" separator on linux system , if you are under windows use ";"

Related

How do I run a java application with maven from command line on ubuntu?

I'm trying to run this java application with maven from command line on ubuntu with OpenJDK 13
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)
The project is created with Intellij Idea.
I guess I've made the build successfully with these commands
git clone https://github.com/danvega/httpclient-tutorial.git
cd httpclient-tutorial
mvn package
However, I don't know how to run the application from command line.
I tried these commands
cd target/classes
java dev.danvega.Application
and got this error
Error: Unable to initialize main class dev.danvega.Application
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/type/TypeReference
What am I missing?
You started Java without specifying where the Maven dependencies can be found, which is called CLASSPATH (and since Java 9 also MODULEPATH if you use the Java Module System), similar how *.dll files can be found in the PATH on windows or LD_LIBRARY_PATH can be used for *.so on Unix-systems
Please see the great answers and questions from others before you:
https://stackoverflow.com/a/20045263/6250649
Maven Run Project
Building a fat jar using maven
Is there a Maven plugin that runs a non-fat jar?
I personally prefer either:
The Maven Exec plugin https://www.mojohaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html
Create a fat jar (e.g. with the Maven Shade plugin) that is also practical for distributing your code and all required dependencies so anyone can easily start with java -jar yourfat.jar (or just click and it will start on Windows)
Here, you have maven project which has one dependency on jackson-databind which in turn will have some more dependencies i.e jackson-core and jackson-annotations.
Classes from these dependencies are not bundled in your application jar, so you cannot just run the Application main class from your project directly using java command, you need to specify the dependent classes on java classpath so that java can load these dependent classes of your program.
Since, it is a maven project, these dependent jars will be pulled into maven default directory (.m2) into your's home path and as you mentioned, you are using ubuntu that will be /home/<your username>/, For example your username which you are logged in with is singularli then your home path must be /home/singularli, you can also check it with echo $HOME command.
So, you would find the maven folder, which stores all the jar(s), into your home /home/singularli/.m2/repository, now here you would find jars like jackson-databind, jackson-core (these will be little inside subdirectories, as it keeps according to the package name, given below command example will give you more idea about it).
At last, once you find these jars, you would need to specify the classpath using -cp flag and include these jars with your application jar which would look like as given below:
java -cp "target/httpclient-tutorial-1.0-SNAPSHOT.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar" dev.danvega.Application
It should work the same way as shown in that video, you referred in your question.
Please notice that you may have different versions i.e com/fasterxml/jackson/core/jackson-annotations/2.11.4, I included 2.11.4 as an example, you may check the version in this project and include that, if different versions are there and you included anyone of them, it may cause some issue as some feature used in this project might not be present in that version
The third-party dependency that contains com/fasterxml/jackson/core/type/TypeReference (which you can find the pom.xml com.fasterxml.jackson.core:jackson-databind is required at both compile time and runtime. If you run using the java command, you need to specify the dependency on the classpath. But since you are using Maven, there is an exec-maven-plugin that you can use for convenience which will handle the classpath at runtime:
mvn exec:java -Dexec.mainClass="dev.danvega.Application"
You can also compile then run in the same command:
mvn package exec:java -Dexec.mainClass="dev.danvega.Application"

run java file in linux command with maven dependencies

I have written a Java program with intellij and it's working, but now I want to run this file with a command, but when I try to compile it with javac I get errors because it doesn't recognise my imports, I'm new to programming so how can I run my program knowing that it has dependencies in maven?
If you have a choice, you should use maven command (mvn) rather than the low-level compiler javac, it makes compiling and packaging much easier.
But if you want to use javac, you must pass the path to the jar your code depends on, as explained in : How do I compile a java file that has jar dependencies?
If you have pulled your dependencies using maven and you are using centos, the jars should be stored in directory ~/.m2
But once again you should consider installing and using maven, if you want to manually compile your code in a similar way Intellij does
Maven was created to make it easier to handle builds with dependencies (jar files) which can be automatically downloaded from the internet. This includes invoking javac with all the dependencies on the build classpath using "mvn compile" - you do not have to invoke javac yourself! Look in target/classes for your byte code files compiled by Maven.
You can also ask Maven to run your Java class with the required dependencies available. Use the following command (adapted as necessary):
mvn exec:java -Dexec.mainClass="com.example.Main"
See http://www.mojohaus.org/exec-maven-plugin/usage.html for more details.

How to build a project using libvirt java binding

it might be a stupid question but how do I set up an environment to develop a test application using libvirt?
do I have to set up ant or maven project or can I just copy java files to my src folder in eclipse ?
Thanks
If you use Eclipse, you can build your classes and run your program in Eclipse without using Ant or Maven. In fact, Eclipse is usually completely ignorant of your Ant build.xml or your Maven pom.xml file. Eclipse uses its own build technology (although you can ask it to run your pom.xml or build.xml). However, a pure Eclipse way of building a project means there is no way to build and run your project except with Eclipse. If you have a continuous integration system, or someone downloads your project and simply wants to build your jar or war file, there's no way they can do it.
Actually, not entirely true. You could write a shell script to compile your code via the javac commands and jar it up via the jar command. What Maven and Ant do is give you a framework to help build your application and remove system dependencies.
Ant has an advantage of flexibility. You can do things easily in Ant that are harder to do in Maven. The disadvantage is that Ant has the flexibility to do things in a really, really bad way. I almost always recommend for developers to use Maven for new projects. It forces them to write their project in a standard way, and eliminates the need to write Ant build scripts which most developers really can't do.
What if you don't know Maven, but know Ant? I still recommend that you use Maven and take this as an opportunity to learn Maven.

working with maven to run java class files

I am new to Maven
My question is where does maven download the external dependency jars in the local machine.
Basically my application uses a lot of external dependencies and i am able to compile the application using maven
But is there a way to run the application from commandline with Java without having to create a Jar-with-dependencies, basically by just using all the class files and specifying my main class. For that i will have to set the classpath to the directory where my external jars are located. How do i do that?
Thanks in advance!!
You can also use the exec maven plugin to run your application inside the maven lifecycle. In its simplest form the command would be
mvn exec:java -Dexec.mainClass=com.example.Main
The full set of configuration options are described on the exec:java page. The classpathScope option might be especially interesting.
The advantage of this method would be that you can configure any command line arguments and system properties in your pom file.
You can use the dependency plugin to ask maven for the classpath. Just run
mvn dependency:build-classpath
and you can use the output in your java -cp command.
Regarding your first question:
My question is where does maven download the external dependency jars in the local machine.
Your local repository is usually here (on a Windows machine)
C:\Users\[username]\.m2\repository
Read more about Maven settings (especially the localRepository setting):
http://maven.apache.org/settings.html

How do I run Google Web Toolkit applications from the command line?

At the moment I'm running the demo StockWatcher in Eclipse. This uses the embedded servlet container which is fine for my purposes. The only change I would like to make is running from the command line instead of inside Eclipse. Does anyone know what command I use and what classpath this requires?
Edit - I have not used maven I have created the project purely using eclipse and gwt plugin
If you used the webAppCreator wizard (I see that you used the Eclipse plugin, but maybe both of them used the same code internally), you should have an ant script (build.xml) generated for you. With it you can run the GWT app via targets so:
ant hosted - run hosted mode (or Development mode, I'm looking at a fairly old ant script)
ant gwtc - compile to JS
ant war, ant build, etc.
See what's in that file (build.xml) to get a better understanding of what is available and what you can do.
Update: see here for documentation on webAppCreator and available ant targets.
With the maven jetty plugin you can run your application with jetty from command line. (You first have to compile & create war with maven gwt plugin I think)
mvn jetty:run
maven gwt plugin
maven jetty plugin
Did you use Maven for this project? If that, then you can run it as mvn gwt:run from command line

Categories