简体   繁体   中英

Open file with external program from classpath (inside Jar)

I'm building a program where I need to open a simple .txt file with the Os's default text reader. I know you can do this with:

Desktop desktop = Desktop.getDesktop();
desktop = open(new File("filename"));

But this opens a file from the System's file path not the class path. Since my program will have to be distributed and passed around, the help file needs to stay within the .jar file.

I tried implementing it with getClass().getResource("/resources/Help.txt")

Like so:

Desktop desktop = Desktop.getDesktop();
desktop = open(new File(getClass().getResource("/resources/Help.txt")));

However, this does not work. I even tried:

Desktop desktop = Desktop.getDesktop();
desktop = open(new File(getClass().getResourceAsStream("/resources/Help.txt")));

But this does not work either.

The Help.txt file is already in the class path, I just need to figure out how to open this file using a external program, however, from within the class path. Is this possible? If yes, how? If not, is there an alternative way to implement this?

..is there an alternative way to implement this?

Extract the bytes out to a temporary file. Then use the Desktop API to display the temporary file.

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