Can't run java code when using package [duplicate] - java

Is there a Maven "phase" or "goal" to simply execute the main method of a Java class? I have a project that I'd like to test manually by simply doing something like "mvn run".

See the exec maven plugin. You can run Java classes using:
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...
The invocation can be as simple as mvn exec:java if the plugin configuration is in your pom.xml. The plugin site on Mojohaus has a more detailed example.
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>

1. Edit POM.xml
Add the following property in pom.xml. Make sure you use the fully qualified class name (i.e. with package name) which contains the main method:
<properties>
<exec.mainClass>fully-qualified-class-name</exec.mainClass>
</properties>
2. Run Command
Now from the terminal, trigger the following command:
mvn clean compile exec:java
NOTE You can pass further arguments via -Dexec.args="xxx" flag.

The above mentioned answers are correct but I am simplifying it for noobs like me.Go to your project's pom file. Add a new property exec.mainClass and give its value as the class which contains your main method. For me it was DriverClass in mainpkg. Change it as per your project.
Having done this navigate to the folder that contains your project's pom.xml and run this on the command prompt mvn exec:java. This should call the main method.

No need to add new plugin in pom.xml. Just run this command
mvn org.codehaus.mojo:exec-maven-plugin:1.5.0:java -Dexec.mainClass="com.example.Main" | grep -Ev '(^\[|Download\w+:)'
See the maven exec plugin for more usage.

Give the Exec Maven plugin a try

clean package exec:java -P Class_Containing_Main_Method command is also an option if you have only one Main method(PSVM) in the project, with the following Maven Setup.
Don't forget to mention the class in the <properties></properties> section of pom.xml :
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.main.class>com.test.service.MainTester</java.main.class>
</properties>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>${java.main.class}</mainClass>
</configuration>
</plugin>
STS Run Configuration along with above Maven Setup:

Related

How to fix "No plugin found for prefix 'liquibase' in the current project and in the plugin groups"

I am new to Liquibase. I have already included maven plugins and liquibase to my pom.xml however when i update liquibase using mvn liquibase:update I get this error:
No plugin found for prefix 'liquibase' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo]
How can I fix this error so that when I type mvn liquibase:update it will run properly
Here are some of the dependencies that are in my pom.xml related to liquibase
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.3</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
</configuration>
</plugin>
</build>
I don't think the problem is with liquibase plugin itself. It's more about maven prefixes.
Try executing: mvn org.liquibase:liquibase-maven-plugin:update
Also, check out this question.
Make sure you are in the right path when you are executing mvn liquibase:update command. And also try to run the command in a separate terminal like command prompt. In my case I'm using the terminal in IntelliJ IDEA when I had the problem but when I tried it in different terminal, it worked flawlessly.
In my case problem was in path. I was trying to start command c:\mvn.cmd liquibase:status from wrong folder.
Running from c:\projects\My_current_project\mvn liquibase:status works like a charm. c:\projects\My_current_project\ is folder, where pom.xml is.
In my case the issue was my pom is configured in such a way that the liquibase plugin is only defined under certain profiles which need to be passed in before the mvn liquibase:<your command here> gets run.
for example, if the pom.xml has a local profile and your liquibase plugin is ONLY defined under that profile, you should run something like mvn -P local liquibase:update in order for maven to pick up liquibase (since it's only defined in that profile)

maven: execute without actually building anything

I have a project with finalised version in pom files , lets say 12.3.45 .
I have built the code for this version some time ago already, all the built jars are in the local maven repo.
Then at some point I have run mvn clean, so all the target folders are being removed.
And now I want to execute some code, as quickly as possible, using mvn exec:java. Preferably without building anything, because why not? all the jars at some point were already built, and I know there were no code changes after that. How can I force maven to execute the code as fast as possible , not recompile anything, and just reuse the jars from the local repo?
Thanks.
If your artifacts are in a local or remote repository you can use them as dependencies.
You can use exec-maven-plugin's options includeProjectDependencies or includePluginDependencies to use them in java execution
https://www.mojohaus.org/exec-maven-plugin/java-mojo.html#includePluginDependencies. includeProjectDependencies option is enabled (true) by default.
You can execute exec-maven-plugin without building anything with mvn exec:java command
Instructions:
To run exec-maven-plugin you would need a main class to run. I assume you have one in your project. If you don't - you need to make a separate project with a main class.
Create a blank maven project.
In the project add exec-maven-plugin configuration. Set the mainClass
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>pack.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Include you artifacts as dependencies to the project
<dependencies>
<dependency>
<groupId>my.group</groupId>
<artifactId>myartifact</artifactId>
<version>12.3.45</version>
</dependency>
</dependencies>
Run mvn exec:java to execute com.my.package.MyMainClass main class from my.group.myartifact artifact
Edits:
includeProjectDependencies option is enabled (true) by default

Running Java project through command line (with Maven dependencies)

I want to run my working Java project through my terminal (mac). It uses Maven to pull in required dependencies. Any help would be very much appreciated.
First I ran the commend:
mvn clean install
Next:
mvn dependency:copy-dependencies
Finally:
cd target/
java -cp MyProject-1.0-SNAPSHOT.jar:dependency Main
Unfortunately the following error is thrown (clearly showing the dependencies are not being used correctly):
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser
at Main$Quote.quoteMachine(Hi.java:21)
at Main.main(Hi.java:12)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.JSONParser ...
I managed to solve the problem by adding this to my POM file:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
After this I simply ran the following commands in my terminal:
mvn clean install
And then:
mvn exec:java
you need to include the json-simple-.jar in your classpath.

Error in Maven building?

After i wrote
mvn -f pom.xml compile exec:java -Dexec.classpathScope=Compile-Dexec.main Class=storm.starter.WordCountTopology
and found this !!
[INFO] One or more required plugin parameters are invalid/missing for
'exec:java'
[0] Inside the definition for plugin 'exec-maven-plugin' specify the
following:
... VALUE
-OR-
on the command line, specify: '-Dstorm.topology=VALUE
If you link your pom.xml then this would be easier. I'm guessing you're using Storm. Have you written your own topologyClass? From the documentation:
topologyClass
The class name of the topology driver (e.g. "com.foo.bar.MyTopology")
Command line override: -Dmaven.storm.topology=
The documentation also gives you the code for your pom but you might want to add exec-maven-plugin to your pom.xml like so:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.domain.yourApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
One thing to note: you need to alter mainClass to match the class in your project that contains the main method you want to execute.
Then you can just run mvn exec:java.

Run a netbeans maven project from a command line

I wrote a maven code in netbeans that in included 6 classes:
ColumnComparator.java
IQC.java
Main.java
MultipleLinearRegression.java
Overlap.java
PSResidualReduction.java
I want to compile and run it on linux terminal. I tried:
javac Main.java ColumnComparator.java IQC.java MultipleLinearRegression.java Overlap.java PSResidualReduction.java
and got compilation error. The problem is that in MultipleLinearRegression class, I used jama package to do Matrix computation, but in command line I do not know how I should modify dependencies. Hopefully you guys can help me.
Just use the exec-maven-plugin.
Add these lines to your pom.xml (you might already have the <build/> and <plugins/> tag there). Make sure to set the <mainClass/> tag to point to your specific main class.
<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>
Then you run your program from command line:
mvn exec:java
This will not affect/change the behavior of your Netbeans setup so you will still be able to run the program from within Netbeans.

Categories

Resources