2

我正在尝试在 Maven 项目之外的某个地方运行一个胖罐子。可运行的 jar 是从 eclipse 创建的(Export-->Runnable jar-->Extract required libraries in generated jar)。这个 jar 在 lib 以及类文件中包含所有依赖项 jar 和外部 jar。

Eclipse 中 Maven 依赖项的快照 --> Maven 依赖项

下面是可执行jar的结构放置为E:/JAR/fatFromEclipse.jar

META-INF/maven/
META-INF/maven/commons-net/
META-INF/maven/commons-net/commons-net/
META-INF/maven/commons-net/commons-net/pom.xml
META-INF/maven/commons-net/commons-net/pom.properties
META-INF/eclipse.inf
META-INF/services/
META-INF/services/javax.tools.JavaCompiler
META-INF/services/javax.servlet.ServletContainerInitializer
META-INF/web-fragment.xml
META-INF/eclipse.inf
META-INF/services/
META-INF/services/javax.tools.JavaCompiler
about.html
ecj.1

WEB-INF/
WEB-INF/lib/
WEB-INF/lib/commons-io-1.2.jar
WEB-INF/lib/commons-net-3.5.jar
WEB-INF/lib/gson-2.2.2.jar

webapp/
webapp/META-INF/
webapp/META-INF/MANIFEST.MF
webapp/WEB-INF/
webapp/WEB-INF/web.xml
webapp/images/
webapp/images/ajax-loader.gif
webapp/images/ajax-loader2.gif
webapp/images/ajax-loader3.gif
webapp/images/ajax-loader4.gif
webapp/jsp/
webapp/jsp/Archive.jsp

com/SBMArchievePurge/App.class

....
and depedencies class files
..

下面是我的Main 类从可执行 jar 执行。

 String contextPath = "/";
 String appBase = "webapp/";  

 Tomcat tomcat = new Tomcat();     
 tomcat.setPort(8081);
 tomcat.getHost().setAppBase(appBase);

 String path=null;
String decodedPath=null;
 try {
    path=App.class.getProtectionDomain().getCodeSource().getLocation().getPath();

    decodedPath = URLDecoder.decode(path, "UTF-8");
        System.out.println("path of Main==>"+decodedPath);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

 ClassLoader cl= App.class. getClassLoader();

 System.out.println("0===>"+cl.getResource("webapp/" ));
 System.out.println("1 getPath===>"+cl.getResource("webapp/").getPath());
 System.out.println("2 toExternalForm===>"+cl.getResource("webapp/").toExternalForm());
 System.out.println("3 toString===>"+cl.getResource("webapp/").toString());
 System.out.println("4. getFile===>"+cl.getResource("webapp/").getFile());


 File f = new File(cl.getResource("webapp/").toString());
 Context webContext = tomcat.addWebapp(contextPath, f.toString());  //here i am getting File Not found Exception


     try {

          Desktop.getDesktop().browse(new URL("http://localhost:8081/jsp/Archive.jsp").toURI());

         } catch (Exception e) {

           e.printStackTrace();
          }


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


     System.out.println("  tomcat started........");


}

我在 运行 jar时遇到错误,如下 所示Context webContext = tomcat.addWebapp(contextPath, f.toString());java -jar fatFromEclipse.jarE:/JAR

    E:\JAR>java -jar fatFromEclipse.jar
Picked up _JAVA_OPTIONS: -Xmx512M
path of Main==>/E:/JAR/fatFromEclipse.jar
clasloader==>sun.misc.Launcher$AppClassLoader@19efb05
0===>jar:file:/E:/JAR/fatFromEclipse.jar!/webapp/
1 getPath===>file:/E:/JAR/fatFromEclipse.jar!/webapp/
2 toExternalForm===>jar:file:/E:/JAR/fatFromEclipse.jar!/webapp/
3 toString===>jar:file:/E:/JAR/fatFromEclipse.jar!/webapp/
4. getFile===>file:/E:/JAR/fatFromEclipse.jar!/webapp/

Oct 11, 2018 3:35:36 PM org.apache.catalina.core.StandardContext setPath
WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
Oct 11, 2018 3:35:36 PM org.apache.catalina.startup.Tomcat getWebappConfigFileFromJar
WARNING: Unable to determine web application context.xml jar:file:\E:\JAR\fatFromEclipse.jar!\webapp
java.io.FileNotFoundException: jar:file:\E:\JAR\fatFromEclipse.jar!\webapp (The filename, directory name, or volume label syntax is incorrect)
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:215)
        at java.util.zip.ZipFile.<init>(ZipFile.java:145)
        at java.util.jar.JarFile.<init>(JarFile.java:153)
        at java.util.jar.JarFile.<init>(JarFile.java:117)
        at org.apache.catalina.startup.Tomcat.getWebappConfigFileFromJar(Tomcat.java:1137)
        at org.apache.catalina.startup.Tomcat.getWebappConfigFile(Tomcat.java:1117)
        at org.apache.catalina.startup.Tomcat.addWebapp(Tomcat.java:540)
        at org.apache.catalina.startup.Tomcat.addWebapp(Tomcat.java:515)
        at org.apache.catalina.startup.Tomcat.addWebapp(Tomcat.java:197)
        at com.SBMArchievePurge.App.main(App.java:89)



Oct 11, 2018 3:35:38 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8081"]
Oct 11, 2018 3:35:39 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Oct 11, 2018 3:35:39 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Oct 11, 2018 3:35:39 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.28
Oct 11, 2018 3:35:39 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[
        at java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.util.concurrent.FutureTask.get(FutureTask.java:188)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
        ... 6 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@39d3d3]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
        at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4854)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4984)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 6 more
Caused by: java.lang.IllegalArgumentException: The main resource set specified [E:\JAR\tomcat.8081\webapp\jar:file:\E:\JAR\fatFromEclipse.jar!\webapp] is not valid
        at org.apache.catalina.webresources.StandardRoot.createMainResourceSet(StandardRoot.java:723)
        at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:684)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 9 more

Oct 11, 2018 3:35:39 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]
        at java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.util.concurrent.FutureTask.get(FutureTask.java:188)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:439)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.startup.Tomcat.start(Tomcat.java:337)
        at com.SBMArchievePurge.App.main(App.java:151)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:924)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 6 more

Exception in thread "main" org.apache.catalina.LifecycleException: Failed to start component [StandardServer[-1]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
        at org.apache.catalina.startup.Tomcat.start(Tomcat.java:337)
        at com.SBMArchievePurge.App.main(App.java:151)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Tomcat]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 2 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:439)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 4 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:924)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 6 more

E:\JAR>

请帮我解决问题。我已经搜索了很多,但我发现大多数解决方案都在 maven 项目的目标文件夹中运行 jar。在这里,我想将 jar 作为一个整体进行分发,以便其他用户只需双击它并在其中运行 Web 应用程序。提前致谢 !

-马尼什

4

0 回答 0