Simple java program to read files inside jar files and run those files .

Hi Everyone ,

Today we will be learning a very interesting topic where in your most of questions related to

How to get a path to a resource in a Java JAR file?
Running the files inside the jar file once packaged?

So the main thing is firstly you need to add your resource folder to your build.

In order to that

1)Right click on the project

2)Project properties

3)Search build and add your resource folder in source

4)Apply and close

bloggJava

Now once clean  and build the project.

Now here is the tricky part , when you export your project as jar and run the program you will still get the java.io null pointer / file not found exception i.e you’ve use the

class.getClassLoader().getResource("index.html");

Here “index.html” is a file inside my resource folder.

What really happens at run time is we do not the get path as its inside the jar and not on the user’s file system.

So what i’ve implemented is

class.getClassLoader().getResourceAsStream("index.html");

 

I’ve got it as stream then read the file line  by line and created a folder(i.e resourcess) on the user system and copied the file.

So now we have a real relative path available so we can do stuff , here i wanted the file (i.e index.html) to be opened on the default browser of operating system.

And now the source code.

package docker;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
public class docker {
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
*
* @author Shubham Shah
*
*/
System.out.println("Welcome to th3Javacode");
docker d = new docker();
try {
InputStream stream = docker.class.getResourceAsStream("/resources/abc.txt");
if(stream==null)
{
System.out.println("Error");
stream = docker.class.getClassLoader().getResourceAsStream("index.html");
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
System.out.println(System.getProperty("user.dir"));
File theDir = new File("resourcesss");
if (!theDir.exists()) {
System.out.println("creating directory: " + theDir.getName());
boolean result = false;
try{
theDir.mkdir();
result = true;
}
catch(SecurityException se){
//handle it
se.printStackTrace();
}
if(result) {
System.out.println("DIR created");
}
}
String st;
File file2 = new File(System.getProperty("user.dir")+"/resourcesss/index.html");
//Create the file
if (file2.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
//Write Content
FileWriter writer = new FileWriter(file2);
while ((st = br.readLine()) != null)
{ System.out.println(st);
writer.write(st);
}
writer.close();
String a="file:///"+System.getProperty("user.dir")+"/resourcesss/index.html";
URI myUri = new URI (a);
System.out.println("uri"+myUri);
Desktop.getDesktop().browse(myUri);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
view raw docker.java hosted with ❤ by GitHub
<!doctype html>
<head>
</head>
<body>
<h1>Hello World</h1>
<h2>Shubham Shah</h2>
</body>
</html>
view raw index.html hosted with ❤ by GitHub

Please feel free to ask any doubts and questions.

If it solve your problem please share 🙂 as sharing is caring.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.