简体   繁体   English

class.getResource(“。”)返回null

[英]class.getResource(“.”) returns null

I am using wsimport in a web service client project to generate artifact classes based on wsdl files. 我正在Web服务客户端项目中使用wsimport来生成基于wsdl文件的工件类。 I am trying to reference locally the wsdl files, by using wsdlLocation attribute. 我正在尝试通过使用wsdlLocation属性在本地引用wsdl文件。 The generated services will try to create an URL based on the path to the package where the service class resides and the value of the wsdlLocation attribute. 生成的服务将尝试基于服务类所在的包的路径和wsdlLocation属性的值来创建URL。

The problem is that the generated service class is using class.getResource(".") in order to get the path to the current directory (the package/directory where the service class is). 问题在于,生成的服务类正在使用class.getResource(".")来获取当前目录(服务类所在的包/目录class.getResource(".")的路径。 If the application is packaged in a jar (as in my case) this code returns null. 如果应用程序打包在jar中(如我的情况),则此代码返回null。 I have noticed that class.getResource("") would in fact return the correct path, but changing the generated code seems like an ugly solution. 我注意到class.getResource(“”)实际上会返回正确的路径,但是更改生成的代码似乎是一个丑陋的解决方案。 I was wondering if there is some way to set the classpath, so that the previous code would return the path to the package/directory where the generated service class resides (as intended)? 我想知道是否有某种方法可以设置类路径,以便前面的代码将路径返回到生成的服务类所驻留的包/目录(按预期)?

Thank you 谢谢

The reason why you could not get a url to a directory inside your jar file is that you forgot to enable "Add directory entries" option when creating your jar file. 无法获取jar文件中目录的URL的原因是,您忘记了在创建jar文件时启用“添加目录项”选项。

If you used eclipse to create the jar file simply enable the "Add directory entries" option. 如果使用eclipse创建jar文件,只需启用“添加目录条目”选项。

If you use jdk jar command I think that option is enabled by default. 如果您使用jdk jar命令,我认为默认情况下该选项已启用。

Do not use regular zip creators such as WinZip, 7zip or WinRar as I don't believe that have this option. 不要使用常规的zip创建程序,例如WinZip,7zip或WinRar,因为我不相信有此选项。

You just use forward slash "/" in place of period or dot "." 您只需使用斜杠“ /”代替句点或点“”即可。 . It was a bug in earlier version but is now resolved. 它是早期版本中的错误,但现在已解决。 Check the following link to know about the bug. 检查以下链接以了解该错误。 If using forward slash doesn't help let me know. 如果使用正斜杠没有帮助,请通知我。 Bug 虫子

I would suggest you to use QName and have it as your property somewhere, it gives you peace of mind as you have the option of changing your wsdl to point to any location, in case you dont want the default relative path based approach. 我建议您使用QName并将其作为某个位置的属性,如果您不想更改默认的基于相对路径的方法,可以选择更改wsdl以指向任何位置,因此可以放心使用。

IMHO I dont see any reason for not changing generated code, though it causes the pain of having a change everytime you regenerate the code. 恕我直言,我看不出没有更改生成的代码的任何原因,尽管这会导致您每次重新生成代码时都要进行更改的痛苦。

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

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