【问题标题】:how can I run a jar from JSP tomcat server如何从 JSP Tomcat 服务器运行 jar
【发布时间】:2016-07-10 00:21:45
【问题描述】:

我创建了一个打印“Hello World”的简单 jar (helloword.jar),我想从在 apache-tomcat 服务器中运行的 JSP 网页运行它。 helloword.jar 文件位于包含我的 JSP 页面的同一项目/文件夹中。

这是我的 JSP 代码,它没有在 tomcat 控制台或 web-broswer 控制台中显示消息“Hello World”。 (我在 NetBeans IDE 的库中导入了 jar)

<%@ page import="helloword.*" %>
<%
        helloword hw = new helloword();
        hw.main();

               %>

我也试过了,但也没有用。

Runtime.getRuntime().exec("java -jar helloword.jar");

【问题讨论】:

标签: java jsp tomcat jar


【解决方案1】:

在创建 jar 时,编写除 main 之外的方法 例如

public static void display()
{
  System.out.println("Hello World!");
}

step1: 将 jar 放入 web-inf lib 文件夹。 step2:在页面标签中导入jar。 step3:直接通过类名调用该方法。

【讨论】:

    【解决方案2】:

    如果要动态执行jar,需要将jar添加到jvm上下文中,然后创建Main类的实例,然后运行main方法。

    URLClassLoader child = new URLClassLoader (myJar.toURL(), this.getClass().getClassLoader());
    Class clazz = Class.forName ("com.MyClass", true, child);
    Method method = clazz.getMethod("methodName", String.class);
    Object o = method.invoke(null, "whatever");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      • 1970-01-01
      • 2014-07-12
      • 2014-11-09
      • 1970-01-01
      相关资源
      最近更新 更多