use html file from jar in java - java

I want to add html files to jar files and access them through jar in the windows application
project how can i access them
I am using the following code .
try{
File f1= new File(getClass.getResource("/path is the src folder/));
}
but it is showing no suitable constructor found

File won't take a URL as reference. Embedded resources like this aren't actually files, they are InputStreams to the resource.
Depending on how you're showing them will determine what you need to do.
For example, JEditorPane takes a URL via its setPage method
UPDATED
You should be able to load the pages directly into the editor pane using something like...
// editor pane is a reference to a JEditorPane
editorPane.setPage(getClass.getResource("/path is the src folder/"));

Related

Cannot Construct An Image Object With Relative File Path WIthout Using file: Prefix

I have been trying to create an image object like this:
Image img = new Image("images/jack.png");
or
Image img = new Image("jack.png");
or /jack.png or /images/jack.pngetc.
I have looked up the working directory using System.getProperty("user.dir") and it is indeed where I put my image file. When I use file: prefix, it does work, like so:
Image img = new Image("file:images/jack.png");
However, it is also supposed to work without using it. In the textbook it is done without file:. I've seen other codes that work without it.
At the end of a bunch of chained exceptions, it says:
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
I also tried to read source code from OpenJDK and I could figure anything out because many methods were native and from what I traced I didn't understand how it didn't work. Also, I can create files the same way, I just can't create images. For instance, this works:
File file = new File("fileName.txt");
What causes this problem, what should I do to fix it?
I'm using NetBeans, if that matters.
Note that System.getProperty("user.dir") does not return the working directory. It returns the user directory.
A path relative to the working directory can be specified using a relative file path in the File constructor. However it's bad practice to rely on the working directory. Starting the application from NetBeans results in the working directory being the project directory, but this is not the case, If started in a different way.
Images you need in your application should therefore be added to the jar.
In this case you can retrieve the image URL via Class.getResource(). (convert to String using toExternalForm().)
If you have a File that references a image file, you can use the File instance to get a URL:
File file = ...
String urlString = file.toURI().toURL().toExternalForm();
Those URLs can be used with the Image constructor.
Note that
File file = new File("fileName.txt");
does not create a file. It just represents a file path. This file may or may not exist. Simply invoking the File constructor does not create a new one.
File file = new File("name.txt");
creates a file somewhere. It doesn't read the existing file whereas
Image image = new Image("pathToImage.png");
tries to read the existing image. In order to be able to read an image stored somewhere you need either the absolute path, which requires the protocol (http, file, ftp etc.) or you put your image into the 'known' directory, like the resources dir of your project.
Say, you have your java sources under src/main/java. The resources dir could be src/main/resources. Put your image there and try working with relative path relative to src/main/resources.

How to create a runnable jar that uses files and images as

I want to understand the best or the standard technique to write a java project when using internal files. To be precise, I want to develop a project that uses text files and images that are needed when the program runs. My goal is to create a runnable jar from the project in which the user does not need to see all these files. Therefore, I decided to create a package called resources and put it inside the folder that contains the source codes. I.e. it is in the same level as other packages. Now, in my codes when I want to use the images I use the following statement:
URL url = getClass().getResource("/resources/image1.gif");
It is working!
Then, to open a text file for reading/writing, I use the following:
String filename= "/resources/"+file1.txt;
Now, this is not working and it complains that it cannot find the file. I am not sure how to go about this?
A google search suggested that I put the resources folder on the project root directory. It is working then but when I created the runnable jar I had to put the resources folder on the same directory as the jar. This means that the user can have access to all the files in there. Any help is much appreciated.
You can use the getResource method for files too.
URL url = getClass().getResource("/resources/file1.txt");
try {
File f = new File(fileUrl.toURI());
} catch (URISyntaxException e) {
//dealing with the exception
}

Java code cannot find resource file after build and clean in Netbeans IDE?

I really need your help to solve my own problem. Now, I'm dealing with small code app. In that project folder contain some resource files (*.xlsx, *.png,...). I placed them in current folder with code file. I just wonder that when I run my code in netbean ide, it just worked find.
After I build code project, I get a jar file in "dist" directory. I run it. It open normally since app used JFrame as user interface. However, when I execute some function of that app, it showed me the error log. Here is the error message:
java.io.FileNotFoundException:
src\sample.xlsx (The system cannot find the path specified)
What's the matter out there?
Here is some pieces of my code:
copyFile(new File("src\\sample.xlsx"),
new File(txtout.getText()+"\\sample.xlsx"));
Node: copyFile function is used for copy file from source to dest.
Here is my project folder structure in Netbean IDE:
Project Name
Source Pakage(src)
myClass.java, sample.xlsx, etc
First, never reference src directly, the directory will not exist once the program is built. Second, you can not access resources which have been embedded within in the application context via a File reference, they simply no longer exist on the file system.
Instead, you need to use Class#getResource or Class#getResourceAsStream
URL url = getClass().getResource("/sample.xlsx");
InputStream is = getClass().getResourceAsStream("/sample.xlsx");
// Don't forget to manage your streams appropriately...
Well you can create a folder named resources under the src folder put your resources in it and use them in your code by using getResourceAsStream() and getResource() methods that can access the embedded resources.Clean and Build will compile the code and embed the contents of the resources folder into the application’s .jar file.
Ways of Accessing resources :
String pathToImage = "resources/images/filling.png";
InputStream stream= ClassName.class.getResourceAsStream(pathToImage );
String pathToImage = "resources/images/filling.png";
InputStream stream= ClassName.class.getResource(pathToImage );
please refer the link information

How to read a resource file in Java standalone applications

I am creating a Java Application where in I need to read templates which I am not able to. That is because I am not able to locate the files with proper path.
The code structure is as follows:
The template reading class file is in src/main/java/com.prototype.main while,
The templates are residing in src/main/java/templates
Which means, when I create a jar the three folders would be 3 folders:
com
templates
META-INF
Now from any class under the package how can I access the templates?
I have tried the following:
new File("/") which in case of web application would have picked from the root directory of the application but in stand alone applications it is the user directory of the system
System.getProperty("user.dir") which gives the project directory
new App().getClass().getName() which shows the package name separated by dot
So if I want to read a template file like /templates/some.xml, how do I get the entire path of this file?
Thanks for any help in advance.
An embedded resource is not a File, it is a resource.
You can read these resources using Class#getResource or Class#getResourceAsInputStream depending on your needs.
For example...
URL url = getClass().getResource("/templates/some.xml");
Will return a URL reference to the named resource.
You can use
InputStream i=this.getClass().getClassLoader().getResourceAsStream("templates/some.xml");
like described in xml FileNotFoundException using slick2D library in java

How to read text files without defining the location?

I'm starting to build a JFrame application to work with File Handling. What I'm trying to get done from the application is that
it reads the contents of all the texts files in a particular location and merges the contents & creates one single text file.
The main property this application should have is that it should not have the navigate-to-location feature. Suppose if I paste this application in location C:\Users\Desktop\application.exe, the application must search the location for all the text files (i.e. on Desktop) & merge them into one single text file.
I've observed this in patch tools to patch softwares, they never ask for location for the software's_launcher.exe, they just tell us to paste the patch in the directory where the launcher belongs.
How do they do it? How can I do the same for my own application?
"./" is to specify current directory.
if you use
File f1 = new File("./");
then f1 is reference of current directory.
if your application is at C:\Users\Desktop\application.exe place then all files & folder at C:\Users\Desktop can access by "./" string

Categories

Resources