java.lang.IllegalArgumentException: URI is not hierarchical – Java classpath error

Published on Author bloggerfive

Problem:

I have written the java class file which reads text file from same class path, parse it and write in output file. If I run this code from eclipse, it will works fine as expected.

This is the code I written to find input file.

//Finds Resource
    URL url = ClassLoader.getSystemResource(“Filename.txt”);

But when I make it as a runnable jar, it throws “URI is not hierarchical – Java classpath error” exception.

Solution:

Instead of that, just replace this code to solve this exception

File inputFile = new File(Thread.currentThread().getContextClassLoader().getResource(“Filename.txt”).getFile());

or

InputStream is = getClass().getResourceAsStream(“Filename.txt”);
Scanner cns = new Scanner(is);

Note :

Eclipses automatically find the current class loader path.

Comments

comments