Results 1 to 5 of 5
  1. #1
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default why I get error while trying to run a class in a package

    I have created two classes in a package:
    1)class MyMaths
    package mytest;

    public class MyMaths {


    public static int add(int op1, int op2)
    {
    return (op1+op2);
    }

    }
    2) class MathTest
    package mytest;
    import junit.framework.*;

    public class MathTest extends TestCase {


    public MathTest(String string) {
    // TODO Auto-generated constructor stub
    super(string);
    }

    public static void testAdd()
    {
    int a=3;
    int b=4;
    int sum=a+b;
    int total=MyMaths.add(a,b);
    assertEquals(sum,total);
    }

    public static void main(String[] args)
    {
    System.out.println("hello");
    //TestCase test= new MathTest("testAdd");
    //test.run();


    }

    }


    these two classes are put in my test folder. I did the following:
    1) cd c:\mytest
    2) javac *.java
    3) java mytest.MathTest
    it gives me error saying:
    NOClassDeffoundError: mytest\MathTest

    I did the following steps:
    4)cd .. (get outside of mytest)
    5) java mytest.MathTest
    again it is Classnotfound but this time the error message is different:
    NOClassDefFOund:junit.framework.TestCase

    my Question:
    1) why I have two different errors when I run the command from different directory?
    2) how to successful run the class?

    thanks!

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Your second attempt, while unsuccessful, appears more successful than the first seeing as Java "found" the classes, but didn't find the junit.framework.TestCase class.

    Question: you don't appear to be setting the classpath when you compile or run your programs, and this may be why Java cannot find the junit.framework.TestClass class. There is a switch for setting the classpath when compiling and running programs and the Sun Java tutorials can tell you all about it.

  3. #3
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    thanks Curmudgeon.

    my classpath is as below:
    c:\program files\java\jdk1.6.0_20\lib\*.jar;.;c:\program files\java\jdk1.6.0_20\lib;d:\JavaCode\test\;d:\Ja vaCode\JunitSeleniumTest\src\test
    where junit jar is under c:\program files\java\jdk1.6.0_20\lib\

    I'll check the sun java tutorial to find out how to set up the classpath. thanks!

  4. #4
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    another question, I am able to compile successfully, while get runtimeerror.
    so I am wondering whether it is because I set the classpath wrongly, or I import the wrong package in the code or include the wrong package in my folder. thanks!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    get runtimeerror.
    Please copy and paste all or error message text here.
    Don't know how to solve the problem without seeing the error message

Similar Threads

  1. I got an error message when using package in Textpad
    By magicianseeker in forum New To Java
    Replies: 5
    Last Post: 12-09-2010, 03:57 AM
  2. Replies: 1
    Last Post: 03-04-2010, 04:07 PM
  3. Replies: 10
    Last Post: 09-01-2009, 10:53 PM
  4. Compiling a class in a package from cmd
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10:27 AM

Posting Permissions

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