Results 1 to 8 of 8
Thread: DrJava Error
- 04-19-2010, 04:11 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
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:
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.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 ); } }
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
- 04-19-2010, 06:11 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you write both class in the same file?
- 04-19-2010, 06:14 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Yes. Both the classes are written in the same file.
- 04-19-2010, 06:15 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 04-19-2010, 06:15 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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); } }
- 04-19-2010, 06:22 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
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?
- 04-19-2010, 06:32 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I think you get it wrong way. Say you've a java file named Test.java, then your content could be like this.
but you cannot do 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"); } }
or 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"); } }
Think about that why it is?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"); } }
- 04-26-2010, 04:16 AM #8
Member
- Join Date
- Oct 2008
- Posts
- 2
- Rep Power
- 0
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
-
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM -
help with drJava
By aw646475 in forum New To JavaReplies: 0Last Post: 03-15-2009, 07:32 PM -
Error Eclipse &drjava displays wrong language
By fenderman in forum New To JavaReplies: 0Last Post: 02-28-2009, 03:24 AM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM


LinkBack URL
About LinkBacks


Bookmarks