bigdata123
bigdata123

Reputation: 463

Exception in thread "main" java.io.FileNotFoundException: when running maven project using java -cp command

I'm facing

Exception in thread "main" java.io.FileNotFoundException: sample (No such file or directory)

when i'm trying to execute java program using

java -cp samplee.jar com.x.y.TestProgram 

I wrote a java program and created a jar using Maven build and input to the program is a file named 'sample' which i kept in project root folder(So i did n't mention the path of file) and com.x.y.TestProgram is the location of my program that has my main method.

Also i'm reading my file inside my java program using

BufferedReader br = new BufferedReader(new FileReader("sample"));

I had read couple of posts describing same exception but does n't seem to be related my issue Need guidance in resolving the issue?

Upvotes: 0

Views: 1194

Answers (2)

horatius
horatius

Reputation: 814

You current Java classpath includes the jar file sample.jar, but not the directory where sample is located. Add the directory "." to your classpath, so that java loads the directory where sample file is located aka "." aka your current directory into your classpath, and then it'll find the sample file.

Upvotes: 0

František Hartman
František Hartman

Reputation: 15086

Your folder structure should look similar to this:

rootFolder/
    pom.xml
    sample
    src/main/java/com/x/y/TestProgram.java
    target/samplee.jar

When you are in a root folder you should be able to execute

java -cp target/samplee.jar com.x.y.TestProgram

and your sample file should be found.

Upvotes: 1

Related Questions