简体   繁体   English

Java:使用OS标准应用程序打开jar中的资源(txt文件)

[英]Java: opening a resource (txt file) which is in a jar with OS standard application

i get the error "AWT-EventQueue-0 java.lang.IllegalArgumentException: URI is not hierarchical". 我收到错误“ AWT-EventQueue-0 java.lang.IllegalArgumentException:URI不分层”。 -I'm trying to use the java.awt.Desktop api to open a text file with the OS's default application. -我正在尝试使用java.awt.Desktop api使用操作系统的默认应用程序打开文本文件。 -The application i'm running is launched from the autorunning jar. -我正在运行的应用程序是从autorunning jar启动的。 I understand that getting a "file from a file" is not the correct way and that it's called resource. 我了解从文件中获取“文件”不是正确的方法,并且将其称为资源。 I still can't open it and can't figure out how to do this. 我仍然无法打开它,也无法弄清楚该怎么做。

open(new File((this.getClass().getResource("prova.txt")).toURI()));

Is there a way to open the resource with the standard os application from my application? 有没有办法从我的应用程序中使用标准os应用程序打开资源? Thx :) 谢谢 :)

您必须将文件从Jar中提取到temp文件夹中,然后打开该临时文件,就像处理Zip文件(Jar基本上是Zip文件)中的文件一样。

You do not have to extract file to /tmp folder. 您不必将文件解压缩到/ tmp文件夹。 You can read it directly using `getClass().getResourceAsStream()'. 您可以使用`getClass()。getResourceAsStream()'直接读取它。 But note that path depend on where your txt file is and what's your class' package. 但请注意,路径取决于txt文件的位置以及类的包。 If your txt file is packaged in root of jar use '"/prova.txt"'. 如果您的txt文件打包在jar的根目录中,请使用'“ /prova.txt”'。 (pay attention on leading slash). (注意斜杠)。

I don't think you can open it with external applications. 我认为您无法使用外部应用程序打开它。 As far as i know, all installers extract their compressed content to a temp location and delete them afterwards. 据我所知,所有安装程序都将其压缩内容提取到临时位置,然后将其删除。

But you can do it inside your Java code with Class.getResource(String name) 但是您可以使用Class.getResource(String name)在Java代码中完成此操作

http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String ) http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String

Wrong 错误

open(new File((this.getClass().getResource("prova.txt")).toURI()));

Right

EULA

/**

Do you accept the License Agreement of XYZ app.?

*/
import java.awt.Dimension;
import javax.swing.*;

import java.net.URL;
import java.io.File;
import java.io.IOException;

class ShowThyself {

    public static void main(String[] args) throws Exception {
        // get an URL to a document..
        File file = new File("ShowThyself.java");
        final URL url = file.toURI().toURL();

        // ..then do this
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JEditorPane license = new JEditorPane();
                try {
                    license.setPage(url);
                    JScrollPane licenseScroll = new JScrollPane(license);
                    licenseScroll.setPreferredSize(new Dimension(305,90));

                    int result = JOptionPane.showConfirmDialog(
                        null,
                        licenseScroll,
                        "EULA",
                        JOptionPane.OK_CANCEL_OPTION);
                    if (result==JOptionPane.OK_OPTION) {
                        System.out.println("Install!");
                    } else {
                        System.out.println("Maybe later..");
                    }
                } catch(IOException ioe) {
                    JOptionPane.showMessageDialog(
                        null,
                        "Could not read license!");
                }
            }
        });
    }
}

如果您要传递的内容可以接受java.net.URL则可以使用:

this.getClass().getResource("prova.txt")).toURI().toURL()

There is JarFile and JarEntry classes from JDK. JDK提供了JarFileJarEntry类。 This allows to load a file from JarFile. 这允许从JarFile加载文件。

JarFile jarFile = new JarFile("jar_file_Name");
JarEntry entry = jarFile.getJarEntry("resource_file_Name_inside_jar");
InputStream stream = jarFile.getInputStream(entry); // this input stream can be used for specific need

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

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