1

This has been asked countless times but I haven't gotten a solution for it. My code:

import java.util.Scanner;
import java.io.*;

public class Reverse
{
public static void main(String[] args)
{
    File myFile = new File(args[0]);
    try
    {
        Scanner input = new Scanner(myFile);
        String message = "";
        while(input.hasNext())
        {
            message = input.nextLine() + message;
        }

        System.out.println(message);
        input.close();
    }
    catch(FileNotFoundException e)
    {
        System.exit(1);
    }
}
}

Details:

  • Being coded through Eclipse on Mac

  • Ran on command line using javac Reverse.java then java Reverse.java (edit: java Reverse works)

- This has something to do with classpaths, but I have no clue what I'm supposed to do

I haven't done anything with regards to classpaths so any help is appreciated.

edit: My question now is, how does java -classpath . Reverse work? I don't really understand the -classpath tag and the '.' tag.

B.A.
  • 37
  • 1
  • 10
  • possible duplicate of ["Error: Could not find or load main class My.class"](http://stackoverflow.com/questions/11473854/error-could-not-find-or-load-main-class-my-class) – Sotirios Delimanolis Apr 06 '14 at 04:15
  • I just ran through your program, I didn't get any error you have mentioned. Run by "java Reverse" instead of "Reverse.java" – Pradeep Simha Apr 06 '14 at 04:15
  • I tried javac Reverse.java then java -classpath . Reverse and it works. Also doing javac Reverse.java then java Reverse works. But I don't really know what is behind -classpath and what it's doing – B.A. Apr 06 '14 at 04:18
  • @B.A., I just updated my answer to address your edit. – merlin2011 Apr 06 '14 at 04:26

1 Answers1

1

As mentioned by Pradeep's comment, you need to run java Reverse instead of java Reverse.java. Otherwise it will look for a class called Reverse.java, which does not exist. Your class is called Reverse.

In response to the editted question, java -classpath . basically tells java to use the current working directory . as part of the classpath. The classpath is the path where java looks for classes to load and run.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • How could you explain that in even simpler terms? Does it mean that classpath is looking into bin and src as well in the project file? – B.A. Apr 06 '14 at 04:28
  • If you are running `java -classpath .` from the command line, it will add only the current directory to your classpath. Note the word *add*, rather than *replace with*. There might be other stuff in your class path depending on your environmental settings, but there no particular reason why `bin` or `src` should or should not be in the classpath if you do not explicitly add them. – merlin2011 Apr 06 '14 at 04:33