Results 1 to 5 of 5
- 05-29-2010, 01:23 AM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
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!
- 05-29-2010, 03:02 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
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.
- 06-01-2010, 07:11 PM #3
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
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!
- 06-01-2010, 07:14 PM #4
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
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!
- 06-01-2010, 08:15 PM #5
Similar Threads
-
I got an error message when using package in Textpad
By magicianseeker in forum New To JavaReplies: 5Last Post: 12-09-2010, 04:57 AM -
Need to know how can we find out the package name and which class to be modified.
By shiva.b in forum New To JavaReplies: 1Last Post: 03-04-2010, 05:07 PM -
package src.Verbs clashes with class of same name
By Addez in forum New To JavaReplies: 10Last Post: 09-01-2009, 11:53 PM -
Compiling a class in a package from cmd
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 11:27 AM
Bookmarks