0

我有一个小项目,我正在使用 shadowJar 创建一个从命令行运行的 fatjar。

主类入口点使用这样的静态工厂方法获取项目中的类的引用

static void main (args){


MessageSystemClient mclient = MessagePlatformFactoryProducer.getFactory().getMessagePlatformInstance("WLS")

println "howdi "....
}

当我在命令行运行 fat jar 时,我得到一个像这样的错误

PS D:\Intellij-projects\message-platform-client\build\libs> java -jar message-platform-client-1.0-SNAPSHOT.jar --send "hello" -r
Exception in thread "main" java.io.FileNotFoundException: file:\D:\Intellij-projects\message-platform-client\build\libs\message-platform-client-1.0-SNAPSHOT.jar!\ApplicationConfig.groovy (The filename, directory name, or volume label syntax is incorrect)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at groovy.util.CharsetToolkit.<init>(CharsetToolkit.java:77)
        at org.codehaus.groovy.runtime.ResourceGroovyMethods.newReader(ResourceGroovyMethods.java:1741)
        at org.codehaus.groovy.runtime.ResourceGroovyMethods.getText(ResourceGroovyMethods.java:592)
        at org.codehaus.groovy.runtime.dgm$1013.doMethodInvoke(Unknown Source)
        at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.doMethodInvoke(GeneratedMetaMethod.java:83)
        at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
        at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:63)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:298)
        at com.softwood.implementation.MessagePlatformFactory.getMessagePlatformInstance(MessagePlatformFactory.groovy:29)
        at com.softwood.client.AbstractMessagePlatformFactory$getMessagePlatformInstance.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
        at com.softwood.cli.Launcher.main(Launcher.groovy:40)

但是,如果我注释掉静态工厂调用 - 并重新生成,那么胖 jar 运行正常并将 howdi 打印到控制台。

如果我再次取消注释工厂查找,只需在 IDE 中运行 Launcher 类,代码就可以正常工作,没有问题

那么为什么它作为fatjar失败,但作为普通项目工作?其次假设它与 fatjar zip 方法有关 - 我该如何克服这个问题?

我有一个项目的早期版本,它直接在项目类上调用静态方法,并且作为 fatjar 工作正常 - 所以问题在于 fatjar 中的静态工厂行为。

我尝试使用加载类

Launcher.getClass().getClassLoader().loadClass ("<various factory classes etc>") 

类可以正常加载 - 但工厂调用本身仍然会中断,如堆栈跟踪中所示

有人可以帮我解决这个问题吗?

4

1 回答 1

0

找到它 - 在我的工厂方法中,我正在执行这样的调用来加载配置文件:

File configFile = new File(classLoader.getResource("ApplicationConfig.groovy")?.getFile()) 

但是,根据相关链接,这在胖 jar 中不起作用,因为 jar 中没有完整的文件系统。

你需要做的是使用如下行:

InputStream configStream = classLoader.getResourceAsStream("ApplicationConfig.groovy")

因为这将在 jar 中工作,并测试流是否为空(在 fatjar 中找不到您的资源),或者您获得流本身,您可以从文件中读取文本。

花了一段时间,但现在我想。

于 2019-03-14T16:35:50.800 回答