I have a spring-boot application which I need to start up by going to the folder directory and start up my web application via command line. I have a class called Application.java and the code inside it is as followed.
#SpringBootApplication(scanBasePackages = {"com.ubs.tas.topcat.dashboard"})
public class Application extends SpringBootServletInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class.getName());
private static final Class<Application> applicationClass = Application.class;
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
public static void main(String[] args) {
LOGGER.info("Starting...");
SpringApplication.run(Application.class, args);
}
}
I set up classpath then tried to run the command "java ApplicationUtility" but I'm getting this error message "Could not find the main class: ApplicationUtility. Program will exist."
I presume you are trying to compile the application and run it without using an IDE.
I also presume you have maven installed and correctly added maven to your environment variable.
To install and add maven to environment variable visit Install maven if you are under a proxy check out add proxy to maven
Navigate to the root of the project via command line and execute the command
mvn spring-boot:run
The CLI will run your application on the configured port and you can access it just like you would if you start the app in an IDE.
Note: This will work only if you have maven added to your pom.xml
You will need to build the jar file first. Here is the syntax to run the main class from a jar file.
java -jar path/to/your/jarfile.jar fully.qualified.package.Application
If you're using gradle, you can use:
./gradlew bootRun
Run Spring Boot app using Maven
You can also use Maven plugin to run your Spring Boot app. Use the below example to run your Spring Boot app with Maven plugin:
mvn spring-boot:run
Run Spring Boot App with Gradle
And if you use Gradle you can run the Spring Boot app with the following command:
gradle bootRun
To run the spring-boot application, need to follow some step.
Maven setup (ignore if already setup):
a. Install maven from https://maven.apache.org/download.cgi
b. Unzip maven and keep in C drive (you can keep any location. Path location will be changed accordingly).
c. Set MAVEN_HOME in system variable.
d. Set path for maven
Add Maven Plugin to POM.XML
`<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
`
Build Spring Boot Project with Maven
mvn package
or
mvn install / mvn clean install
Run Spring Boot app using Maven:
mvn spring-boot:run
[optional] Run Spring Boot app with java -jar command
java -jar target/mywebserviceapp-0.0.1-SNAPSHOT.jar
1.Run Spring Boot app with java -jar command
To run your Spring Boot app from a command line in a Terminal window you can use java -jar command.
This is provided your Spring Boot app was packaged as an executable jar file.
java -jar target/app-0.0.1-SNAPSHOT.jar
2.Run Spring Boot app using Maven
You can also use Maven plugin to run your Spring Boot app. Use the below command to run your Spring Boot app with Maven plugin:
mvn spring-boot:run
3.Run Spring Boot App with Gradle
And if you use Gradle you can run the Spring Boot app with the following command:
gradle bootRun
Spring Boot provide the plugin with maven.
So you can go to your project directory and run
mvn spring-boot:run
This command line run will be easily when you're using spring-boot-devs-tool with auto reload/restart when you have changed you application.
A Spring Boot project configured through Maven can be run using the following command from the project source folder
mvn spring-boot:run
For macOS user
If you configured your application using maven
First, go to your project directory in the terminal
then simply run
./mvnw spring-boot:run
You are done. Now you can hit localhost with port number.
This command mentioned in the spring official guide.
One of the ways that you can run your spring-boot application from command line is as follows :
1) First go to your project directory in command line [where is your project located ?]
2) Then in the next step you have to create jar file for that, this can be done as
mvnw package [for WINDOWS OS ] or ./mvnw package [for MAC OS] , this will
create jar file for our application.
3) jar file is created in the target sub-directory
4)Now go to target sub directory as jar was created inside of it , i.e cd target
5) Now run the jar file in there.
Use command java -jar name.jar [ name is the name of your created jar file.]
and there you go , you are done . Now you can run project in browser,
http://localhost:port_number
For Maven based projects use this command to run the spring boot application:
mvn spring-boot:run
I was having the same problem, I fixed it by modifying the dependency in the pom. I added web at the end.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-**starter**</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
open cmd from your project path the then at cmd run following cmmands-
1st way to build and run->
mvn clean install
for run only ->
mvn spring-boot:run
1st way to build and run after jar file generated in target folder ->
java -jar target/accounts-0.0.1-SNAPSHOT.jar
Related
I am new to maven, I have some .hbs (handle-bar template files), I have "handlebars-proto", which includes premade handlebar server.
https://github.com/jknack/handlebars.java (search for handlebars-proto)
I've added the dependency to my pom.xml as
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars-proto</artifactId>
<version>${current-version}</version>
</dependency>
Documentation states, I should be able to run it using,
java -jar handlebars-proto-${current-version}.jar -dir myTemplates
How can I create a Intellij Run Configuration to do same as above. I tried creating JAR task runner & using "handlevar-proto-{version}.jar" but it's unable to find the jar file. In general how to run a jar from the maven in IntelliJ.
There is option in run configurations to configure JAR Application
I have two main classes in my Maven based project (Spring Boot). I'm using <start-class> property in pom.xml to indicate the Spring Boot main class. The other one is a process that I need to run manually sometimes.
This is the project's structure:
Project (pom.xml dir) is in ~/projects/coolproject
Packages (containing the classes) are under ~/projects/coolproject/src/main/java
Main class I want to execute manually is ~/projects/coolproject/src/main/java/com/company/Process.java
After run mvn package, I want to run Process.class, but I'm getting Could not find or load main class error. I have tried this:
From ~/projects/coolproject/src/main/java, execute java com.company.Process
From ~/projects/coolproject/src/main/java, execute java -cp . com.company.Process
From ~/projects/coolproject/src/main/java/com/company, execute java Process
From ~/projects/coolproject/, execute java -cp target/coolproject-1.0.0-SNAPSHOT.jar com.company.Process
with no success. How can I do it?
I have tried to run it using Maven Exec plugin. From pom.xml's dir:
$ mvn exec:java -Dexec.mainClass="com.company.Process"
But it runs the other main class (Spring Boot's main class), instead. It's ignoring the mainClass param.
As mentioned in comments, while using spring boot, you should use start-class instead of mainClass as the parameter in exec plugin. Try:
mvn exec:java -Dstart-class=com.company.Process
Alternatively, if you want to use java -cp to run your main class, you need to pass the correct dependencies. It seems that coolproject-1.0.0-SNAPSHOT.jar isn't built with dependencies that com.company.Process needs. To build a jar with all dependencies, you can use maven-assembly-plugin. Then you can try:
mvn package
java -cp target/coolproject-1.0.0-SNAPSHOT-jar-with-dependencies.jar com.mycompany.Process
I'm using FlywayDB for migrations on a Maven Java project. I am currently working to make it possible to deploy to Heroku.
On my local machine I am using the Maven Flyway plugin to run migrations:
$> mvn clean compile flyway:migrate
To do the same on heroku, I would normally try:
$> heroku run mvn flyway:migrate
However, mvn is not available after the build phase, so this yields an error (mvn: command not found)
How can I run my flyway migrations on Heroku?
I think your best bet is to create a small class in your application that uses the FlywayDB Java API. It might look like this:
class Migrator {
public static void main(String[] args) throws Exception {
...
Flyway flyway = new Flyway();
flyway.setDataSource(url, user, password);
flyway.migrate();
}
}
Then create an entry in your Procfile like this:
migrate: java -cp target/classes:target/dependency/* Migrator
And finally run it as needed with heroku run migrate.
The reason Heroku does not include Maven in the slug (i.e. at runtime) is because the .m2 directory is not retained. If Maven were included, and you then ran a mvn command, it would first have to download the internet. The .m2 directory is not retained because it would make the slug size too large.
According to the Heroku documentation using the Maven plugin is not recommended for running Flyway migrations.
Within the documentation there are two alternatives:
Running Flyway automatically with Spring
Running Flyway with
the Java API (Also see #codefinger's answer)
I'm working on a web service locally, built using maven and deploying to tomcat. I'm using the maven plugin to run locally, using mvn tomcat:run as my run configuration.
Right now, my service is being deployed using the default project name as a context path:
http://localhost:8080/myArtifactId/servletPath
I would instead like to deploy to remove the context path, and deploy to this url through configuring the tomcat maven plugin:
http://localhost:8080/servletPath
Per this documentation: http://tomcat.apache.org/maven-plugin-2.1/tomcat7-maven-plugin/usage.html
I am trying this:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<!-- Why isn't this working??? -->
<path>/</path>
</configuration>
</plugin>
Any ideas on why what I'm trying isn't working, or a different way I can go about this? As I've stated, I'm trying to do this through the maven plugin.
Alternatively, does anyone know how to pass this in as an argument to the run configuration? Maybe passing it in explicitly at the command line will override the default.
UPDATE: Running mvn tomcat7:run or mvn clean tomcat7:run from the command line appears to make this work as intended. But when I run using a Maven Build run configuration in Eclipse, the service is started using the default artifact ID, regardless of what I put in the path variable in my pom.xml.
UPDATE: This appears to be a problem with the Eclipse Maven plugin. When I run the command from the command line, everything works as expected but when trying to add an Eclipse/Maven run configuration with a Tomcat7:run goal, the project keeps running using the default artifact id context path.
When experiencing problems with maven they mostly get solved by using the clean plugin (mvn clean) which cleans out your project's working directory.
http://maven.apache.org/plugins/maven-clean-plugin/
I am playing to Maven and tried to built a simple HellowWorld application.
This application uses Spring to libraries.
When I tried to run it, I run it through:
\target\classes
with command:
java -cp HelloWorldApp
It has a long list of classpath dependencies.
I think maven must have some more clever ways to do this instead of listing a whole list of dependency libs.
Can someone help?
Update:
Thanks. I now have another question. I run the project using:
mvn exec:java -Dexec.mainClass="com.vaannila.HelloWorldApp"
However, my project uses a Spring config called beans.xml which is in the
\src\main\resources
When I run it, it says:
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:142)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.jav
a:336)
How can I specify where to look the Spring config?
Many thanks
Run your application by issuing mvn exec:java at the command line, maven will take care of the rest including download of the maven exec plugin.
EDIT As for your updated question: It appears that maven did not copy your resources to the target folder, you can use the maven-resources-plugin to do that. This link should help you get this done.
If you use IDE, such as eclipse +m2eclipse - it will calculate all dependencies from maven dependencies.
If you are running from command line use Exec maven plugin