JavaFX WebView不会在jar中加载上层文件夹脚本 [英] JavaFX WebView does not load upper folder script in jar

查看:108
本文介绍了JavaFX WebView不会在jar中加载上层文件夹脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建基于JavaFX WebView的代码编辑器。

 <!DOCTYPE html>>我将jquery加载到通过WebView加载的.html文件中。 
< html lang =en>
< head>
< meta charset =UTF-8>
< title>代码编辑器< / title>
< link type =text / css =stylesheethref =styles.css/>
< script type =text / javascriptsrc =../ libs / jquery-3.1.1.js>< / script>
< script type =text / javascriptsrc =scripts.js>< / script>
< / head>
< body>
< div id =content>
加载中...
< / div>
< / body>
< / html>

所以,这个文件被加载到WebView,但jquery没有加载! script.js加载但jquery-3.1.1.js不是!我建议问题在文件系统中的某处。我的文件存储为jar文件资源,所以我的index.html路径如下所示:

  jar:file:/ D: /Pe3oHaHc/YandexDisk/Projects/Mira/Mira/build/libs/Mira-0.0.1.jar!/windows/html/editor/scripts.js 

我在我的scripts.js中通过以下代码获得它:

  var scripts = document.getElementsByTagName(script); 
alert(scripts [1] .src);

所以,scripts.js加载了,但jquery-3.1.1.js不是因为它在上层文件夹!而且我不能在scripts.js中使用$。



但是,如果我将jquery-3.1.1.js放在与index.html相同的文件夹中,它会很好地加载。我猜它是因为jar文件里面的奇怪路径。你可以帮我吗?我真的不想把jquery文件放到index.html的同一个文件夹中。

解决方案

jar协议没有允许 .. 相对位置说明符。

从jar加载的资源加载相对url的请求将使用与用于加载原始资源相同的协议。在这种情况下,因为原始资源是从jar中加载的,所以使用 jar:协议。您可以在 JarURLConnection中找到jar协议的定义类文档。



您可以使用不同的协议来加载资源,例如 http: file:,在这种情况下 .. 将以这些协议理解 .. 的方式工作。要做到这一点,您需要从jar文件中提取相关资源并将它们放置在Web服务器上或本地文件系统。这可能不是你想要的。

一个简单的解决方案是不要在你的html文件中使用 .. ,要么把它放在一个井下在jar中使用已知的根目录并使用绝对引用(例如 /libs/jquery-3.1.1.js ),或者将这些项包括在与html相同的文件夹中或其子文件夹(例如 jquery-3.1.1.js libs / jquery-3.1.1.js )。



我知道这不是您想要的答案,但我没有准确的解决方案来完成您想要的功能。



相关问题:


Im trying to create code editor based on JavaFX WebView. I'm loading jquery to my .html file loaded by WebView like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Code editor</title>
    <link type="text/css" rel="stylesheet" href="styles.css"/>
    <script type="text/javascript" src="../libs/jquery-3.1.1.js"></script>
    <script type="text/javascript" src="scripts.js"></script>
</head>
<body>
    <div id="content">
        Loading...
    </div>
</body>
</html>

So, this file is loaded to WebView, but jquery is not loaded! script.js loaded but jquery-3.1.1.js is not! I'm suggesting the problem lying somewhere in file system. My files is stored as jar-file resources, so my index.html path is like:

jar:file:/D:/Pe3oHaHc/YandexDisk/Projects/Mira/Mira/build/libs/Mira-0.0.1.jar!/windows/html/editor/scripts.js

I get it by this code in my scripts.js:

var scripts = document.getElementsByTagName("script");
alert(scripts[1].src);

So, scripts.js loaded, but jquery-3.1.1.js is not cause it is in upper folder! And i cannot use $ in scripts.js.

But if i put my jquery-3.1.1.js in the same folder as a index.html it loaded well. I guess its because the strange path inside jar file. Can you help me? I'm realy don't want to place jquery file into same folder to index.html.

解决方案

The jar protocol does not allow for .. relative location specifiers.

A request to load a relative url from a resource loaded from a jar will use the same protocol as was used to load the original resource. In this case, because the original resource is loaded from a jar, the jar: protocol is used. You can find the definition of the jar protocol in the JarURLConnection class documentation.

You could use a different protocol to load the resources, e.g. http: or file:, in which case .. will work as those protocols understand ... To do so, you would need to extract the relevant resources from the jar file and host them on a web server or local file system. Which is probably not what you want.

A simpler solution is to not use .. in your html files, either by placing the items under a well-known root directory within the jar and using an absolute reference (e.g. /libs/jquery-3.1.1.js), or including the items in the same folder as the html or a subfolder of it (e.g. jquery-3.1.1.js or libs/jquery-3.1.1.js).

I know this is not the answer you wanted, but I don't have an exact solution to do precisely what you want.

Related question:

这篇关于JavaFX WebView不会在jar中加载上层文件夹脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆