getResource()找不到文件?(getResource() not finding file?)
当尝试在Eclipse中加载文件时,文件加载得很好,但是当我使用jar-splice将项目打包到.JAR文件时,似乎应用程序无法再找到其资源文件。
这是加载文件的方法:
public static File loadFile(String path) throws FileNotFoundException { InputStream stream; stream = FileUtil.class.getClassLoader().getResourceAsStream(path); System.out.println("Stream = " + stream); //Debug purposes File file = new File(FileUtil.class.getClassLoader().getResource(path).getFile()); if (!file.exists()) { System.err.println("Path: " + FileUtil.class.getClassLoader().getResource(path).getPath()); //Also debug purposes throw new FileNotFoundException(); } return file; }
使用这两个System.out.printlns,很明显应用程序无法根据该路径找到该文件,但如果您查看该图片,那么该路径正是它所查找文件所在的位置。 我很困惑,因为之前从未发生过这种情况,而且它所说的无法找到文件的路径正是它的确切位置。 任何人的想法?
When trying to load the file in Eclipse the file loads just fine, however when I package the project into a .JAR file using jar-splice it seems that the application can no longer locate its resource files.
Here's the error thrown when the application is run
And here is the method that loads files:
public static File loadFile(String path) throws FileNotFoundException { InputStream stream; stream = FileUtil.class.getClassLoader().getResourceAsStream(path); System.out.println("Stream = " + stream); //Debug purposes File file = new File(FileUtil.class.getClassLoader().getResource(path).getFile()); if (!file.exists()) { System.err.println("Path: " + FileUtil.class.getClassLoader().getResource(path).getPath()); //Also debug purposes throw new FileNotFoundException(); } return file; }
Using those two System.out.printlns it's clear that the application can't find the file based on that path, but if you look at the picture that path is exactly where the file it's looking for is located. I am so confused as this has never happened before, and the path it's saying it can't find the file at is exactly where it is. Any ideas anyone?
原文:https://stackoverflow.com/questions/36731493
最满意答案
首先在你的js中设置Bloodhound:
var dataSetBloodhound = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'), queryTokenizer: Bloodhound.tokenizers.whitespace, local: data });
其中
data
是数组中的建议列表。
我的例子是data = [ { name: "Foo", url: "/bar.jpg" }, etc, etc ]
这就是我在
Bloodhound.tokenizers.obj.whitespace('name')
使用name的原因,因为我希望我的建议成为数据数组中的name
。在我的HTML中我有我的输入:
<input id="quick-search-input" type="text" class="form-control" placeholder="Products" data-provide="typeahead"/> //The important thing here is 'data-provide="typeahead"'
哪个是建议框将作用的输入。
然后在它后面设置js:
$('#quick-search-input').typeahead({ hint: true, highlight: true, minLength: 1 }, { name : 'NameForFormInput', displayKey: 'name', templates: { suggestion: function(data) { return '<li class="list-group-item"> <p class="predictionText">'+data.name+'</p></li>'; } }, source : dataSetBloodhound });
我认为这是你出错的地方,因为我在设置时遇到了类似的问题,但在我实现模板时修复了它。 此外,css将根据您的使用情况而有所不同。
First in your js set up the Bloodhound:
var dataSetBloodhound = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'), queryTokenizer: Bloodhound.tokenizers.whitespace, local: data });
Where
data
is the suggestion list in an array.
Mine for example isdata = [ { name: "Foo", url: "/bar.jpg" }, etc, etc ]
this is why I use name in
Bloodhound.tokenizers.obj.whitespace('name')
because I want my suggestions to be thename
in the data array.In my html I have my input:
<input id="quick-search-input" type="text" class="form-control" placeholder="Products" data-provide="typeahead"/> //The important thing here is 'data-provide="typeahead"'
Which is the input that the suggestion box will act upon.
Then setting up the js behind it:
$('#quick-search-input').typeahead({ hint: true, highlight: true, minLength: 1 }, { name : 'NameForFormInput', displayKey: 'name', templates: { suggestion: function(data) { return '<li class="list-group-item"> <p class="predictionText">'+data.name+'</p></li>'; } }, source : dataSetBloodhound });
I think this is where you are going wrong as I had a similar issue when setting it up but fixed it when I implemented a template. Also the css will be different depending on what your using.
相关问答
更多-
如何提交从先行猎犬自动完成填充的数据(How do I submit data populated from typeahead bloodhound auto complete)[2022-06-14]
Typeahead.js没有“更新程序”选项。 相反,你可以编写一个事件处理程序,它将使用typeahead的选定事件(即typeahead:selected),例如 var numSelectedHandler = function (eventObject, suggestionObject, suggestionDataset) { alert(suggestionObject.num); }; typeahead.on('typeahead:selected', numSelectedHa ... -
我实现了Bloodhound.get()如下(也见这个小提琴: http : //jsfiddle.net/Fresh/HS9Wy/ ): // instantiate the bloodhound suggestion engine var numbers = new Bloodhound({ datumTokenizer: function (d) { return d; }, queryTokenizer: Bloodhound.tokenizers.white ...
-
Typahead Bloodhound提示和自动完成不使用远程URL(Typahead Bloodhound hint and autocomplete not working with remote url)[2022-12-06]
您可以在远程对象上使用Bloodhound转换方法来模拟为本地数据实现的相同行为。 window.bh = bloodhoundSuggestions = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), queryTokenizer: Bloodhound.tokenizers.whitespace, sufficient: 3, local: su ... -
你可以将Bloodhound的遥控器设置为这样的东西: remote: { url: '/equipment/getSuggestions/%EQUIPMENT/%QUERY', replace: function(url) { return url.replace('%EQUIPMENT', $('#equipment-type-input').val()).replace('%QUERY', $('input.typeahead.tt-input').val()); }, fil ...
-
JSON文件包含一个JSON对象数组,但Bloodhound建议引擎需要一个JavaScript对象数组。 因此,您需要在预取声明中添加一个筛选器: prefetch: { url: '../data/test.json', filter: function(names) { return $.map(names, function(name) { return { name: name }; }); } 至于“datumTokenizer”,其目的是确定基准(即建议值)应如何标记。 ...
-
猎犬重置本地数据(bloodhound resetting local data)[2022-11-04]
我认为你需要重新初始化,你可以设置 if( checked == "Spray") { // Clear the object dataSetBloodhound.clear(); // Set the local 'data' to spray dataSetBloodhound.local = spray; // Reinit the object dataSetBloodhound.initialize(t ... -
首先在你的js中设置Bloodhound: var dataSetBloodhound = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'), queryTokenizer: Bloodhound.tokenizers.whitespace, local: data }); 其中data是数组中的建议列表。 我的例子是 data = [ { name: "Foo", url: ...
-
使用Bloodhound搜索(Using search with Bloodhound)[2021-12-07]
开箱即用,Bloodhound标记器适用于基本类型的数组。 您有“复杂”数据(具有2个属性的对象),因此您必须告诉Bloodhoud使用obj tokenizer进行令牌化并传递属性名称: datumTokenizer: Bloodhound.tokenizers.obj.whitespace("id", "name"), Out of the box, Bloodhound tokenizers work for an array of a basic type. You have "complex" ... -
Bloodhound识别bug?(Bloodhound identify bug?)[2023-08-28]
回答我自己的问题。 是的,猎犬有一个错误。 SearchIndex.getIntersection()函数未正确实现。 您可以取出此功能并像这样测试: getIntersection([1,2,15],[1,2]) 它应该返回[1,2]作为结果,但事实上它返回[1]。 这是因为它错误地使用sort()函数来排序数字。 根据w3schools: 默认情况下,sort()方法按字母顺序和升序将值排序为字符串。 这适用于字符串(“Apple”出现在“Banana”之前)。 但是,如果数字被排序为字符串,则“25 ... -
Searchkick + Bloodhound + Typeahead用于自动完成(Searchkick + Bloodhound + Typeahead for autocomplete)[2022-03-15]
您需要返回哈希以响应自动完成操作: def autocomplete render json: Doctor.search(params[:query], autocomplete: true, limit: 10).map {|doctor| {name: doctor.name, value: doctor.id}} end You need to return hash in response of your autocomplete action: def autocomplete ren ...