Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

errors in the code

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
0
0
IN
hi, i am learning java...

i want to know how i can debug in a java code.

if its a C code....then i do some print() statements and exit(1) for debugging.
usuing those print statements i can find out my error easily .


but i think there is no such exit(1) like keyword in java. is it????

then how to debug in a java code?

recently i have compiled a java code sucessfully...but at the runtime i am getting

"....Exception in thread "main" java.lang.NoSuchMethodError: main
Press any key to continue... " ---------error.

i dont know what is this. how can i fix this??

will you tell what is the procedure to debug a java code

thanks...
 
exit(1) in c is System.exit(1);

Debug in Java can be obtained by the stack trace.

Your particular error is saying that there is (as the runtime error states) "no such method". I am guessing that you tried to exectute a command on the console such as "java MyClass" - but you have not implemented a "main" method - the entrypoint of a class executed by the console command line. You need the class to look like this :

Code:
public class MyClass {
  public MyClass() {
    System.out.println("Default constructor called");
  }

  public callSomeMethod() {
     System.out.println("Hello there");
  }

 public static void main(String args[]) {
    // construct your new MyClass instance
    MyClass mc = new MyClass();
    // Call a method within that class
    mc.callSomeMethod();
 }

}
 
but i have a main() method....i am giving the code below...its compiling.

Code:
class Car {

  String licensePlate = "";    // e.g. "New York 543 A23"
  double speed        = 0.0;   // in kilometers per hour
  double maxSpeed     = 120.0; // in kilometers per hour
  
  // accelerate to maximum speed
  // put the pedal to the metal
  void floorIt()
  
  {
    this.speed = this.maxSpeed;  
  }
  
 
  void accelerate(double deltaV) 
  
  {

     this.speed = this.speed + deltaV;
     if (this.speed > this.maxSpeed) 
     {
       this.speed = this.maxSpeed; 
     }
     if (this.speed <  0.0) {
       this.speed = 0.0; 
     }     
     
  }
  
}


class CarTest4 

{

  public static void main(String[] args) {
  Car c = new Car();
    
    c.licensePlate = &quot;New York A45 636&quot;;
    c.maxSpeed = 123.45;
    
    System.out.println(c.licensePlate + &quot; is moving at &quot; + c.speed + 
     &quot; kilometers per hour.&quot;);

    for (int i = 0; i < 15; i++) {
      c.accelerate(10.0);     
      System.out.println(c.licensePlate + &quot; is moving at &quot; + c.speed + 
       &quot; kilometers per hour.&quot;);
    }

  }
    
}

i am copying and pasting from a java tutorial site. whats wrong with this????
 
How are you running the code on the command line - like &quot;java CarTest4&quot; ?
 
i am usuing jcreator.i did

tool>compile file>execute file

 
Yes but which class are you executing ??!

The class Car has no main method - but CarTest4 does. Are you executing CarTest4 ? Whatever you think you are doing, the error message says there is no method - so you cannot be executing the CarTest4 class.

Are they separate files - ie CarTest4 is not an inner class of Car is it ?
 
ok....this time i am usuing command line

what i did ....

c:\java\bin\ javac d:\CarTest4.java----->compiled succesfully

but...

c:\java\bin\ java CarTest4---->&quot; Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: CarTest4 &quot;

Are they separate files ???--> that is single code.whatever i have posted that i have copied from the screen. and the file name is CarTest4.java
 
As a test, I copied and pasted your code in a CarTest4.java file, and it compiles and runs fine; no errors. Your last error (java.lang.NoClassDefFoundError) is probably due to a bad classpath. Make sure your classpath includes the directory which contains CarTest4.class
 
gorodish is correct.

Before you run &quot;java CarTest4&quot; on the command line, first type this :

set CLASSPATH=%CLASSPATH%;.
 
already i had set the path.......i went to control panel>...>enviornment variable>edit> c:\javac\bin;

anyway, scite editor is giving me the result...(probabily it searches the system path).

but Jcreator and Command line fails to give the result!!!!!

however... usuing Scite i am getting the result. is there any rule that once i set my path in enviornment variable i cannot use command line????otherwise how command line fails!!! i am explicitly compiling and running from command prompt but still it fails!!!!
 
Its not the path variable - its the CLASSPATH variable.

Please do as I suggested, and before you type &quot;java CarTest4&quot;, type :

&quot;set CLASSPATH=%CLASSPATH%;.&quot;
 
ok i typed at
c:\java\bin set CLASSPATH=%CLASSPATH%; ....still there is same problem

and also tried with c:\set CLASSPATH=%CLASSPATH%;

...no successes

and also c:\&quot;set CLASSPATH=%CLASSPATH%;&quot;--->but this is not accepted.
 
You are fogetting the &quot;.&quot; at the end of the set statement.

set CLASSPATH=%CLASSPATH%;.


(where &quot;.&quot; is the current directory, or the directory where your classes live - ie :

set CLASSPATH=%CLASSPATH%;C:\java\mycode
 
yes...its working now.

i did..

set CLASSPATH=%CLASSPATH%;C:\mycode
then compiled going to bin dir.

and for running i went to c:\mycode then java CarTest4...giving me output..

but i will prefer scite....i donot have to type in the command line.... GUI's are doing my job nicely.

thank you
 
I'm glad you finally got there !

I would suggest taking a basic java tutorial (there are some at as this stuff is all explained step by step there (and in the SDK download's install documentation).

Also, I would advise against using anything apart from a simple text editor (notepad, textpad, vi) for learning java until you are completely competent with the basics (such as paths, classpaths, the compiler, interpreter etc). Using visual editors as a beginner just hides the basics and fundamentals from you, and makes you fall into traps such as we have experienced here.

Good Luck for the future !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top