Results 1 to 8 of 8

Thread: DrJava Error

  1. #1
    pratzy is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Exclamation DrJava Error

    Hi!

    I am new to J2SE & I have recently installed DrJava IDE as I came to know that it consumes very less memory. I have kept all my programs in D:\JAVAPROG.
    Whenever I run a piece of code like the following, I get an error.

    "Welcome to DrJava. Working directory is D:\JAVAPROG
    > java HelloComponent
    Static Error: No static method in HelloComponent with name 'main' accepts arguments (String[])
    > "

    The code is as follows:

    Java Code:
     import java.awt.*;
     import javax.swing.*;
    
        class HelloComponent extends JComponent {
          public void paintComponent( Graphics g ) {
            g.drawString( "Hello, Java!", 125, 95 );
          }
        }
    
    
        public class HelloJava {
          public static void main( String[] args ) {
            JFrame frame = new JFrame( "New Frame" );
            HelloComponent hello = new HelloComponent(  );
            frame.add( hello );
            frame.setSize( 300, 300 );
            frame.setVisible( true );
          }
        }
    The above program compiles without any error but gives error when I run it. However, when I run a code having a single class, it gets executed perfectly. Therefore, I think the problem only occurs in a program with multiple classes where all the classes cannot contain main method.

    Also, if I try to compile and execute the above program in DOS mode, it runs without any trouble. Looks like a DrJava config issue to me.

    How do I overcome this trouble?? HELP!
    Last edited by Eranga; 04-19-2010 at 06:10 PM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    pratzy is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Yes. Both the classes are written in the same file.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You cannot create two classes like that, rule 1: you cannot have two public class in the same *.java file. rule 2: main method should be in the *.java class which you define, if you've multiple classes in the same *.java file.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I think this is what you want to do.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    class HelloComponent extends JComponent {
    
        @Override
        public void paintComponent(Graphics g) {
            g.drawString("Hello, Java!", 125, 95);
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("New Frame");
            HelloComponent hello = new HelloComponent();
            frame.add(hello);
            frame.setSize(300, 300);
            frame.setVisible(true);
        }
    }

  6. #6
    pratzy is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks! This works!

    U said that two classes cant be used in the same .java file. But my program executes pretty well in the DOS mode. How does that work?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by pratzy View Post
    U said that two classes cant be used in the same .java file. But my program executes pretty well in the DOS mode. How does that work?
    I think you get it wrong way. Say you've a java file named Test.java, then your content could be like this.

    Java Code:
    public class Test {
        public static void main(String[] args) {
             MyNewClass objClass = new MyNewClass();
             objClass.printMsg();
        }
    }
    
    class MyNewClass {
       public void printMsg() {
           System.out.println("message");
       }
    }
    but you cannot do this,

    Java Code:
    class Test {
        public static void main(String[] args) {
             MyNewClass objClass = new MyNewClass();
             objClass.printMsg();
        }
    }
    
    public class MyNewClass {
       public void printMsg() {
           System.out.println("message");
       }
    }
    or this,

    Java Code:
    public class Test {
        public static void main(String[] args) {
             MyNewClass objClass = new MyNewClass();
             objClass.printMsg();
        }
    }
    
    public class MyNewClass {
       public void printMsg() {
           System.out.println("message");
       }
    }
    Think about that why it is?


  8. #8
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    There are a number of problems here:
    1. A class you want to run (using java MyClass) has to be public.
    2. You can only have one public class per file, and the file has to have the same name as the public class.

    These two problems are Java restrictions.

    Hope this helps.
    Last edited by htowninsomniac; 04-26-2010 at 04:40 AM.

Similar Threads

  1. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  2. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  3. help with drJava
    By aw646475 in forum New To Java
    Replies: 0
    Last Post: 03-15-2009, 07:32 PM
  4. Error Eclipse &drjava displays wrong language
    By fenderman in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 03:24 AM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •