2

My file name is Temp.java and inside it I have this. I'm using eclipse IDE

/*package*/ class Test {
    public static void main(String args[]) {
        System.out.println("test");
    }
}

So I was unable to run this as java application. I change my class name to Temp

class Temp {
 ....
}

Now I can. Can someone explain me why ?

allprog
  • 16,540
  • 9
  • 56
  • 97
someone
  • 6,577
  • 7
  • 37
  • 60

5 Answers5

6

This is probably a limitation of Eclipse. The code runs well from command line.

As I understand, you are trying to embed your unit tests in the same file with the class under test. This is a nice idea and I totally concur with it. You can read more about how you can succeed in Ben J. Christensen's blog post. Generally, he suggests placing the tests in a static inner class, not a standalone class in the same file.

An example from the Netflix Hystrix framework: HystrixCircuitBreaker.UnitTest

allprog
  • 16,540
  • 9
  • 56
  • 97
1

The class (which main should be run) inside the .java file must have the same name as the file. If the class is not public (as in your case) the class will compile but it can't be run since Eclipse tries to load the class according to the file name.

Kai
  • 38,985
  • 14
  • 88
  • 103
  • 3
    This is true if the class is public, you can put numerous non-public classes into a .java file. – Juvanis Dec 06 '12 at 11:41
  • @null OK, it is not public so it compiles. But you have asked why it is not running. – Kai Dec 06 '12 at 11:53
1

The code below, located in Temp.java, compiles and runs fine with Netbeans:

class Whatever {

    public static void main(String[] args) {
        System.out.println("hello");
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
1

The problem is with eclipse, i think you are trying to run using right click -> run as -> Java Application, unfortunately eclipse is not showing this option if the class is not public. But you can still run the class using Alt+Shift+X,J. Its not the problem with Java, its with Eclipse.

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38
  • +1 for The problem is with eclipse ,,, it not showing if the class name and file name is different, not related to public. – someone Dec 06 '12 at 16:59
1

The name of the file should be the same as the class name which is public and has the main() method. In your first case the file name Temp.java will compile and will create Test.class file not Temp.class because there is no Temp class declared in your file.

after .class file is created , run it with java Test

so here's an example

//Filename abc.java
public class hi
{
public static void main(String[] args)
{
 System.out.println("Hell");
}
}

the output

abc.java:1: class hi is public, should be declared in a file named hi.java
public class hi
       ^
1 error

but if you do this

 //Filename abc.java
    class hi
    {
    public static void main(String[] args)
    {
     System.out.println("Hell");
    }
    }

it will create hi.class file so

D:\>java hi
Hell
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
  • http://stackoverflow.com/questions/13739925/could-not-find-or-load-main-class-in-win7-or-exception-in-thread-main-java-lan/13740004#comment18882499_13740004 – Bhavik Shah Dec 06 '12 at 12:20