java - How to make an executable jar? -
i'm creating new web application using maven. have got code 'spring's guides online creates database. reason, code never being ran.
in pom.xml, have included following code:
<properties> <start-class>hello.application</start-class> </properties>
and 'application' class got spring guides.
package hello;
import java.sql.resultset; public class application { public static void main(string args[]) { // simple ds test (not production!) simpledriverdatasource datasource = new simpledriverdatasource(); datasource.setdriverclass(org.h2.driver.class); datasource.setusername("sa"); datasource.seturl("jdbc:h2:mem"); datasource.setpassword(""); jdbctemplate jdbctemplate = new jdbctemplate(datasource); system.out.println("creating tables"); jdbctemplate.execute("drop table customers if exists"); jdbctemplate.execute("create table customers(" + "id serial, first_name varchar(255), last_name varchar(255))"); string[] names = "john woo;jeff dean;josh bloch;josh long".split(";"); (string fullname : names) { string[] name = fullname.split(" "); system.out.printf("inserting customer record %s %s\n", name[0], name[1]); jdbctemplate.update( "insert customers(first_name,last_name) values(?,?)", name[0], name[1]); } system.out.println("querying customer records first_name = 'josh':"); list<customer> results = jdbctemplate.query( "select * customers first_name = ?", new object[] { "josh" }, new rowmapper<customer>() { @override public customer maprow(resultset rs, int rownum) throws sqlexception { return new customer(rs.getlong("id"), rs.getstring("first_name"), rs.getstring("last_name")); } }); (customer customer : results) { system.out.println(customer); } }
}
my project structure follows:
project name
src
webapp
hello
- application.java
i pretty new can't see why not finding application.java file. ideas appreciated.
edit: whole pom.xml
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org /2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>embed.tomcat.here</groupid> <artifactid>embedtomcatnew</artifactid> <packaging>war</packaging> <version>0.0.1-snapshot</version> <name>embedtomcatnew maven webapp</name> <url>http://maven.apache.org</url> <properties> <start-class>hello.application</start-class> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalname>embedtomcatnew</finalname> <plugins> <plugin> <groupid>org.apache.tomcat.maven</groupid> <artifactid>tomcat7-maven-plugin</artifactid> <version>2.2</version> <configuration> <port>9966</port> </configuration> </plugin> </plugins> </build> </project>
edit: should mention running jsp file - has 'hello world' in prints browser - running want run java class first.
war
you building .war
file, evidenced in <packaging>war</packaging>
definition, deployable web application container. there no startup class, , documented on stackoverflow there way control order of startup in web app containers.
jar
you have change project executable .jar
, specify main
class in manifest
in jar plugin
configuration options. setting random property isn't going anything.
you want use shade
plugin bundle transient dependencies monolithic .jar
otherwise have classpath
installation nightmare on hands.
here example, running src/main/webapp
dir bad non-portable idea, should passed in argument.
import java.io.file; import org.apache.catalina.startup.tomcat; public class main { public static void main(string[] args) throws exception { string webappdirlocation = "src/main/webapp/"; tomcat tomcat = new tomcat(); //the port should run on can set environment variable //look variable , default 8080 if isn't there. string webport = system.getenv("port"); if(webport == null || webport.isempty()) { webport = "8080"; } tomcat.setport(integer.valueof(webport)); tomcat.addwebapp("/", new file(webappdirlocation).getabsolutepath()); system.out.println("configuring app basedir: " + new file("./" + webappdirlocation).getabsolutepath()); tomcat.start(); tomcat.getserver().await(); } }
Comments
Post a Comment