1

I am literally compiling the same java file in both command line and NetBeans but I get different results. It is maddening and confusing. The code itself is simple and it is correct in command line. I get "Error" in NetBeans. This should not vary as far as I can tell...

import java.io.*;
public class A3

{
    public static void main(String[] args)
    {
        BufferedReader fileReader = null;

        try
        {
            fileReader = new BufferedReader(new FileReader("input.txt"));
            String aLine = fileReader.readLine();
            String myTokens[] = aLine.split(" ");

            System.out.println("Token 1 = " + myTokens[0]);
            System.out.println("Token 2 = " + myTokens[1]);

            fileReader.close();
        }
        catch(IOException anException)
        {
            System.out.println("Error");
        }
    }
}

SOLVED! I was way overthinking things. Command line looks for the .txt file in the same directory as the .java. Which is logical. NetBeans looks in the root directory. Which is...yeah.

Cordre
  • 11
  • 3
  • 3
    Well...at least tell what's the error? – Konstantin Yovkov Jan 28 '16 at 10:38
  • Have u checked that the file name is thesame as the class name? – Blaze Jan 28 '16 at 10:39
  • What are error are you seeing on the console? Tell us the error then we will help you! – Blaze Jan 28 '16 at 10:39
  • does it have a package statement, or is it in the default package? – Stultuske Jan 28 '16 at 10:39
  • May be with `input.txt`. – SatyaTNV Jan 28 '16 at 10:40
  • 1
    Tested it in Netbeans 8.1 and it didn't throw an error. Can you please state the error you get? – ctst Jan 28 '16 at 10:41
  • do you supply arguments when you run it in Netbeans? – MaVRoSCy Jan 28 '16 at 10:42
  • 1
    `new FileReader("input.txt")` depends on the current directory - in the command line it is the directory your are in - in Netbeans it may be something else. Can you try with a full path instead? – assylias Jan 28 '16 at 10:46
  • Do you get the error at runtime or at compile time? The question's title implies that you're getting it at compile time, but then you don't specify what is the error you're getting, and this is a little confusing I think. – francesco foresti Jan 28 '16 at 10:53
  • The file is in the same directory, I didn't copy the line but it does have a package, and the "Error" it gives me is literally that line. Which I had set up as an exception in the code itself. Not a specific compile error. run: Error BUILD SUCCESSFUL (total time: 0 seconds) – Cordre Jan 28 '16 at 11:06

1 Answers1

0

Error occurs at line (You can use debugger):

fileReader = new BufferedReader(new FileReader("input.txt"));

You can have a look: Where does java look for files?

Community
  • 1
  • 1
user2173372
  • 483
  • 7
  • 17