gpt4 book ai didi

java - 以编程方式执行服务器 Jar (Caucho Resin)

转载 作者:行者123 更新时间:2023-12-01 11:59:16 24 4
gpt4 key购买 nike

我已经成功使用了 Caucho Resin Web 服务器,并且正在考虑将其打包到 java 应用程序中。他们提供了一个 jar 文件和代码,以便以编程方式启动服务器。

Example: example/TestResin.java

package example;

import com.caucho.resin.*;

public class TestResin {

public static void main(String []args)
{
ResinEmbed resin = new ResinEmbed();

HttpEmbed http = new HttpEmbed(8080);
resin.addPort(http);

WebAppEmbed webApp = new WebAppEmbed("/", "/var/www/htdocs");
resin.addWebApp(webApp);

resin.start();
resin.join();
}
}

但是我收到以下错误

java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at com.caucho.server.resin.EnvInit.init(EnvInit.java:49)
at com.caucho.server.resin.EnvInit.<init>(EnvInit.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:374)
at com.caucho.loader.Environment.initializeEnvironment(Environment.java:971)
at com.caucho.resin.ResinEmbed.start(ResinEmbed.java:320)
at com.mycompany.server.Resin.main(Resin.java:23)
Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 22 more
Exception in thread "main" java.lang.NoClassDefFoundError: javax/enterprise/inject/spi/BeanManager
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at com.caucho.env.service.ResinSystem.<init>(ResinSystem.java:138)
at com.caucho.env.service.ResinSystem.<init>(ResinSystem.java:99)
at com.caucho.server.resin.Resin.<init>(Resin.java:236)
at com.caucho.server.resin.ResinEmbedded.<init>(ResinEmbedded.java:46)
at com.caucho.resin.ResinEmbed.start(ResinEmbed.java:322)
at com.mycompany.server.Resin.main(Resin.java:23)
Caused by: java.lang.ClassNotFoundException: javax.enterprise.inject.spi.BeanManager
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 18 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

解决方案:

添加了在 glassfish 安装目录中找到的 javaee-16.jar 库和所有作品。显然从 Oracle 下载 glassfish-api.jar 不起作用,因为只包含方法声明而没有实现。

最佳答案

使用它来解释 jar,就像使用 java -jar ... 运行它一样:

File f = new File("resin-3.1.0/lib/resin.jar");
URL url = new URL("file", null, f.getAbsolutePath());
ClassLoader cl = new URLClassLoader(new URL[] {url});
Class<?> mcls = cl.loadClass("a.b.c.Main");
Method mmethod = mcls.getMethod("main", new Class[] {String[].class});
mmethod.invoke(mcls);

在这里,Resin 将在它自己的类加载器中运行。

关于java - 以编程方式执行服务器 Jar (Caucho Resin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28108015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com