在Netbeans中工作但不在命令行上

在Netbeans中工作但不在命令行上

本文介绍了访问jar中的资源(在Netbeans中工作但不在命令行上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有捆绑资源(语言模型二进制文件)的jar文件,需要在运行时加载。 jar中的目录结构是

I have a jar file with bundled resources (language model binary files) that need loading at run time. The directory structure within the jar is

tagger / app.class

tagger / models / stop / SentDetect.bin.gz

tagger/app.class
tagger/models/stop/SentDetect.bin.gz

其中SentDetect.bin.gz是一个二进制whos路径作为String参数加载到第三方类(SentDetector),即

where SentDetect.bin.gz is a binary whos path is loaded into a thirdparty class (SentDetector) as a String parameter, i.e.

URL url = this.getClass().getResource("models/stop/SentDetect.bin.gz");
SentenceDetector sdetector = new SentenceDetector(url.getPath());

虽然它在Netbeans中运行正常,但当我尝试从命令行运行它时,我在构造函数中获取FileNotFound异常。我已经仔细检查过二进制文件是否包含在已编译的Jar文件中。

While it runs ok in Netbeans, when I try to run it as a jar from command line, I get a FileNotFound Exception at the constructor. I have double checked that the binary is included in the compiled Jar file.

我认为解决方案通常是使用getResourceAsStream()将数据作为输入流加载,但这不是一个选项,因为url正在传递作为第三方构造函数的String参数,这让我相信问题是如何将url解析为String?

I believe the solution would usually be to load the data in as an input stream using getResourceAsStream(), but this is not an option here, as the url is being passed as a String parameter to a third party constructor, which leads me to believe the problem is with how the url is being parsed to a String?

我试过:

url.getPath();
url.getFile();
url.toURI().getPath();
url.toString();

并且所有人都提供了不同的文件路径。

and all are giving different paths to the file.

推荐答案

听起来应该没问题,如果二进制文件肯定是在正确的位置并且jar文件肯定在阶级路径。 (我假设它可以找到类本身?我希望如此,假设你使用 this.getClass())。

It sounds like that should be fine, if the binary is definitely in the right place and the jar file is definitely in the class path. (I assume it can find the class itself? I'd expect so, given that you're using this.getClass()).

可能导致问题的一件事是文件名大小写 - 当它从文件系统加载文件时,如果你使用的是不区分大小写的Windows;在jar文件中它将区分大小写。检查代码中的大小写与jar文件中的大小写匹配。

One thing which might be causing issues is filename casing - when it's loading the file from the file system, if you're using Windows that'll be case insensitive; in a jar file it'll be case sensitive. Check the case in code matches the case in the jar file.

这篇关于访问jar中的资源(在Netbeans中工作但不在命令行上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 15:34