class.getResource(".") 返回 null

发布于 2024-12-20 00:45:01 字数 368 浏览 4 评论 0 原文

我在 Web 服务客户端项目中使用 wsimport 来生成基于 wsdl 文件的工件类。我尝试使用 wsdlLocation 属性在本地引用 wsdl 文件。生成的服务将尝试根据服务类所在包的路径和 wsdlLocation 属性的值创建 URL。

问题是生成的服务类使用 class.getResource(".") 来获取当前目录的路径(服务类所在的包/目录)。如果应用程序打包在 jar 中(如我的情况),则此代码返回 null。我注意到 class.getResource("") 实际上会返回正确的路径,但更改生成的代码似乎是一个丑陋的解决方案。我想知道是否有某种方法来设置类路径,以便前面的代码将返回生成的服务类所在的包/目录的路径(按预期)?

谢谢

I am using wsimport in a web service client project to generate artifact classes based on wsdl files. I am trying to reference locally the wsdl files, by using wsdlLocation attribute. 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.

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). If the application is packaged in a jar (as in my case) this code returns null. I have noticed that class.getResource("") would in fact return the correct path, but changing the generated code seems like an ugly solution. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感受沵的脚步 2024-12-27 00:45:01

您无法获取 jar 文件中目录的 url 的原因是您在创建 jar 文件时忘记启用“添加目录条目”选项。

如果您使用 eclipse 创建 jar 文件,只需启用“添加目录条目”选项即可。

如果您使用 jdk jar 命令,我认为该选项默认启用。

不要使用常规的 zip 创建程序,例如 WinZip、7zip 或 WinRar,因为我不认为它们有此选项。

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.

If you used eclipse to create the jar file simply enable the "Add directory entries" option.

If you use jdk jar command I think that option is enabled by default.

Do not use regular zip creators such as WinZip, 7zip or WinRar as I don't believe that have this option.

并安 2024-12-27 00:45:01

我建议您使用 QName 并将其作为您的财产在某个地方,它让您安心,因为您可以选择更改您的 wsdl 以指向任何位置,以防万一您不想要默认值基于相对路径的方法。

恕我直言,我看不出有任何理由不更改生成的代码,尽管它会导致每次重新生成代码时进行更改的痛苦。

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.

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.

咽泪装欢 2024-12-27 00:45:01

您只需使用正斜杠“/”代替句点或点“。” 。
这是早期版本中的一个错误,但现已解决。检查以下链接以了解该错误。如果使用正斜杠没有帮助,请告诉我。
错误

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文