Skip to content
Advertisement

Running Java from terminal : cannot find text files

I am using Java in Eclipse for file manipulation like editing, searching, etc. For instance I have two text files. One is “sales.txt” and the other is “employees.txt”. The user is supposed to input a beginning date and ending date as arguments. Then the program finds the dates that match and all between. From there it computes the commission for the employees in which it found the sale dates for.

Here’s the problem: I am using Kali Linux (Debian) and I have to create a shell script that compiles and runs the java program with command line arguments.

For example: the shell script is called “Runner”. So I type: ./Runner [start date] [end date]

Upon doing so it compiles fine with no warnings. But when the program is executed the terminal shows this: cannot find employees or sales .txt But when I use the Run Configurations in Eclipse and edit the Arguments panel for [start date] [end date] the program runs fine.

Here is what the Projects panel looks like in eclipse:

Edit – Here is the test shell script:

#! /bin/bash

javac /$HOME/workspace/Java Projects/Database_2/src/src/*.java
cd /$HOME/workspace/Java Projects/Database_2/src
java src.Runner $1 $2

Edit 2 – Terminal Output > ./testRunner 01/01/2015 01/07/2015

java.io.FileNotFoundException: employees.txt (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.util.Scanner.<init>(Scanner.java:611)
    at src.CommissionModifier.parseEmployees(CommissionModifier.java:101)
    at src.CommissionModifier.process(CommissionModifier.java:15)
    at src.Runner.handleArguments(Runner.java:10)
    at src.Runner.main(Runner.java:6)
java.io.FileNotFoundException: sales.txt (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.util.Scanner.<init>(Scanner.java:611)
    at src.CommissionModifier.parseSales(CommissionModifier.java:135)
    at src.CommissionModifier.process(CommissionModifier.java:16)
    at src.Runner.handleArguments(Runner.java:10)
    at src.Runner.main(Runner.java:6)

Advertisement

Answer

In eclipse, the working folder is the project folder. Looking at your script, that’s probably /$HOME/workspace/Java Projects/Database_2. I’d bet your files are in that folder too, which is why it works in eclipse.

When you run from the script, you are changing the folder to /$HOME/workspace/Java Projects/Database_2/src which probably doesn’t have your files.

If those assumptions are true, your script needs to cd into /$HOME/workspace/Java Projects/Database_2 instead. Java probably won’t be able to find your classes from there, so you’ll need to add the classpath to the command line arguments too.

#! /bin/bash

javac /$HOME/workspace/Java Projects/Database_2/src/src/*.java
cd /$HOME/workspace/Java Projects/Database_2
java -cp src src.Runner $1 $2
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement