简体   繁体   English

写一个jar文件实用程序

[英]writing a jar file utility

I am writing a utility jar file to be shared into multiple project. 我正在编写一个实用程序jar文件,以便共享到多个项目中。 Basically it handles establishing database connection and making calculations and later return the values. 基本上它处理建立数据库连接并进行计算,然后返回值。 Other projects will import my jar file and call my function. 其他项目将导入我的jar文件并调用我的函数。

Right now i am having problem when i tried to call one of the functions in the jar file.In eclipse there are no compile error but as i test it at app server i receives this error 现在我有问题当我试图调用jar文件中的一个函数。在eclipse中没有编译错误但是因为我在app服务器测试它我收到此错误

22:16:35,177 DEBUG ExecutionContext:187 - Transitioning to lifecycle stage RequestComplete
22:16:35,178  WARN DefaultExceptionHandler:94 - Unhandled exception caught by the Stripes default exception handler.
net.sourceforge.stripes.exception.StripesServletException: ActionBean execution threw an exception.
    at net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:183)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: com/my/rnd/test/utils/SimplePing
    at com.app.action.JbpmProcessInstanceActionBean.testTrigger(JbpmProcessInstanceActionBean.java:83)
    at com.app.action.JbpmProcessInstanceActionBean.loadProcInstanceList(JbpmProcessInstanceActionBean.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

I export the jar file using eclipse and here's the sample function of the SimplePing 我使用eclipse导出jar文件,这是SimplePing的示例函数

public class SimplePing {

     /**
      * @param args
      */
     public  void testSimplePing() {
        // TODO Auto-generated method stub
        System.out.println("This is a simple ping .");
     }

}

It seems that the appserver unable to find my class even to it exists in the jar file.Any help is welcomed 似乎appserver无法找到我的类,即使它存在于jar文件中。欢迎任何帮助

If you look closely to the error stack you can see the following: 如果仔细查看错误堆栈,可以看到以下内容:

Caused by: java.lang.NoClassDefFoundError: com/my/rnd/test/utils/SimplePing

Which is a common error when you have one of the following conditions: 当您具有以下某个条件时,这是一个常见错误:

  1. You have a jar(s) missing in your lib 您的lib缺少一个jar(s)
  2. You have some jar(s) missing from your classpath 您的classpath缺少一些jar(s)
  3. You have some dependencies missing from your MANIFEST file: 您的MANIFEST文件中缺少某些依赖项:

    ...
    Main-Class: fully.qualified.MainClass
    Class-Path: lib/dependency1-XYjar lib/dependency2-WZjar
    ...

  4. You have a JDK problem 你有一个JDK问题

Eventually all connect to the same problem: A missing jar ( JDK or third-party related jar) 最终所有连接到同一个问题:一个丢失的jar(JDK或第三方相关的jar)
You can also use maven plugin for eclipse which will manage your dependancies of your current project and your jar able one which is needed by your project, ofcourse if you are fond of Maven. 您还可以使用maven插件进行eclipse,它可以管理您当前项目的依赖性以及您的项目所需的jar ,如果您喜欢Maven的话。

This is a good question 这是一个很好的问题

There are 2 ways I can think of 我有两种方法可以想到

  1. Try to use Maven (m2e plugin) - this will manage the dependencies for you If you do this, remember to add Maven Dependencies to the project Deployment Assembly (right click - properties) 尝试使用Maven (m2e插件) - 这将为您管理依赖项如果您这样做,请记住将Maven依赖项添加到项目Deployment Assembly (右键单击 - 属性)

  2. Use Deployment Assembly and add the other project 使用Deployment Assembly并添加其他项目

使用7-zip或winzip等程序打开jar,并验证您是否在其中看到此目录结构和文件:com / my / rnd / test / utils / SimplePing.class

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

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