繁体   English   中英

如何制作可执行jar?

[英]How to make an executable jar?

我正在使用Maven创建一个新的Web应用程序。 我从网上的“春季指南”中获得了一些代码,这些代码创建了一个数据库。 但是由于某种原因,该代码从未运行过。

在我的pom.xml中,包含以下代码:

   <properties>
    <start-class>hello.Application</start-class>    
   </properties>

这是我从春季指南中获得的“ Application”类。

package hello;

 import java.sql.ResultSet;

public class Application {

public static void main(String args[]) {
    // simple DS for test (not for 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(";");
    for (String fullname : names) {
        String[] name = fullname.split(" ");
        System.out.printf("Inserting customer record for %s %s\n", name[0], name[1]);
        jdbcTemplate.update(
                "INSERT INTO customers(first_name,last_name) values(?,?)",
                name[0], name[1]);
    }

    System.out.println("Querying for customer records where first_name = 'Josh':");
    List<Customer> results = jdbcTemplate.query(
            "select * from customers where 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"));
                }
            });

    for (Customer customer : results) {
        System.out.println(customer);
    }
}

}

我的项目结构如下:

项目名

  • SRC

    • Web应用程序

      • 你好

        • application.java

我对此很陌生,但是我看不出为什么找不到application.java文件。 任何想法,将不胜感激。

编辑:这是整个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>

编辑:我还应该提到它正在运行一个jsp文件-它只包含打印到浏览器的'hello world'-因此它正在运行,但我希望它首先运行Java类。

战争

<packaging>war</packaging>定义所示,您正在构建一个.war文件,该文件只能部署到Web应用程序容器。 没有启动类,并且在stackoverflow上也有记录,有一种方法可以控制大多数Web应用程序容器中的启动顺序。

您必须将项目更改为可执行的.jar并在jar plugin配置选项的Manifest中指定main类。 只是设置一些随机属性不会做任何事情。

您可能还想使用shade插件将所有瞬态依赖项捆绑到一个整体的.jar ,否则您将classpath安装的噩梦。

这是一个示例,从src/main/webapp dir运行此命令是一个不好的不可移植的想法,应将其作为参数传递。

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 that we should run on can be set into an environment variable
        //Look for that variable and default to 8080 if it 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 with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

        tomcat.start();
        tomcat.getServer().await();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM