简体   繁体   中英

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". -I'm trying to use the java.awt.Desktop api to open a text file with the OS's default application. -The application i'm running is launched from the 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? Thx :)

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

You do not have to extract file to /tmp folder. You can read it directly using `getClass().getResourceAsStream()'. But note that path depend on where your txt file is and what's your class' package. If your txt file is packaged in root of jar use '"/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)

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. This allows to load a file from 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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