1

Just started using DrJava, and I'm getting an Illegal Class Literal error when I try to run my code. My code compiles, and no issue arises until runtime. Even just the following leads to the error when I run 'java Percolation(5)' in the interaction terminal.

public class Percolation {
    public Percolation(int N)  {
        System.out.println(N);
    }    
}

I couldn't find anything on Google; any ideas what could be going on here? I feel like I'm missing something stupid. Thanks!

jtimmins
  • 367
  • 3
  • 13

2 Answers2

1

You need a main method in your class to be able to run it. http://docs.oracle.com/javase/tutorial/getStarted/application/

Then if you want to pass an argument to the main method, you call it via

java Percolation 5

not

java Percolation(5)
khelwood
  • 55,782
  • 14
  • 81
  • 108
1

To run your program, you have to call a starting or main method. This method as to be public static void and it has to take VarArgs or an Array for your arguments:

public static void main (String[] args) {
    // in here, you can do stuff like calling other functions, or creating objects

    // no checks, just demo how to use the args:
    Perlocation p = new Perlocation(Integer.parseInt(args[0]));
}

Then you can call your program via

java Percolation 5
ifloop
  • 8,079
  • 2
  • 26
  • 35
  • Careful with Integer#decode, though. It does funny things with leading zeros. I'd use Integer#valueOf or Integer#parseInt – Thilo Oct 02 '14 at 07:40
  • Awesome, thanks. In the past I've used BlueJ for courses, which breaks things up, and I guess hides away the main method. Thus, I've never come across a main method before. Thanks. – jtimmins Oct 02 '14 at 16:12