当 Web 服务器从 launch4j 生成的可执行文件运行时,我在运行带有 Jersey Resources 的 Grizzly Web 服务器时遇到问题。资源在 launch4j 生成的可执行文件中,但程序无法访问它们。如果我直接从 jar 执行它,它运行没有问题。但我需要提供一个 .exe 来运行网络服务器。
网络服务器代码:
String uri = buildURI();
HashMap<String,String> initParams = new HashMap<String,String>();
initParams.put(PackagesResourceConfig.PROPERTY_PACKAGES, "my.packages.resources");
initParams.put(WebAppResourceConfig.FEATURE_XMLROOTELEMENT_PROCESSING, "true");
initParams.put(FeaturesAndProperties.FEATURE_FORMATTED, "true");
HttpServer httpServer = GrizzlyWebContainerFactory.create(uri, initParams);
httpServer.start();
我在 my.packages.resources 中有一个名为 InfoRestResource 的类,代码如下:
@Path("/info")
public class InfoRestResource {
@GET
@Path("/time")
@Produces(MediaType.TEXT_PLAIN)
public String getTime() {
return String.valueOf(new Date().getTime());
}
}
launchj4 Maven 配置:
<plugin>
<groupId>org.bluestemsoftware.open.maven.plugin</groupId>
<artifactId>launch4j-plugin</artifactId>
<version>1.5.0.0</version>
<executions>
<execution>
<id>launch4j</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<dontWrapJar>false</dontWrapJar>
<headerType>console</headerType>
<jar>${basedir}/target/my-jar-with-dependencies.jar</jar>
<outfile>${basedir}/target/program.exe</outfile>
<errTitle></errTitle>
<cmdLine></cmdLine>
<chdir></chdir>
<priority>normal</priority>
<downloadUrl>http://java.com/download</downloadUrl>
<supportUrl></supportUrl>
<customProcName>true</customProcName>
<stayAlive>false</stayAlive>
<manifest></manifest>
<singleInstance>
<mutexName>MyLaunch4jMutex</mutexName>
<windowTitle></windowTitle>
</singleInstance>
<jre>
<path></path>
<minVersion>1.6.0</minVersion>
<maxVersion></maxVersion>
<initialHeapSize>64</initialHeapSize>
<maxHeapSize>512</maxHeapSize>
</jre>
</configuration>
</execution>
</executions>
</plugin>
输出
当我运行 .exe 时:
WebServer started at http://localhost:9200/program/
Scanning for root resource and provider classes in the packages:
my.packages.resources
Initiating Jersey application, version 'Jersey: 1.6 03/25/2011 01:14 PM'
The ResourceConfig instance does not contain any root resource classes.
service exception:
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:103)
at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1178)
at com.sun.jersey.server.impl.application.WebApplicationImpl.access$600(WebApplicationImpl.java:159)
at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:693)
at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:690)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:690)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:685)
at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:601)
另一方面,当我从 Jar 运行它时,它工作正常,我得到以下输出:
WebServer started at http://localhost:9200/program/
Scanning for root resource and provider classes in the packages:
my.packages.resources
Root resource classes found:
class my.packages.resources.InfoRestResource
我真的很感激任何帮助解决这个问题!谢谢!