0

I wrote Test.java to test out a database connection.

I compiled using successfully using "javac Test.java."

But, I can't run my program using "java -cp ..."

>AssetMgmtDbTest me$ pwd
/Users/me/Workspace/tmp/AssetMgmtDbTest
>AssetMgmtDbTest me$ ls
Test.class  Test.java   classes12.jar   ojdbc6.jar
>AssetMgmtDbTest me$ java -cp ojdbc6.jar Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Please advise.

Thanks, Kevin

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

2 Answers2

2

Add the current directory to your classpath:

>AssetMgmtDbTest me$ java -cp ojdbc6.jar:. Test

Note the additional :. (colon + dot) after the jar.

If you start using packages, things will get more complicated because the directory structure is important. Please refer to this answer for details.

Community
  • 1
  • 1
pb2q
  • 58,613
  • 19
  • 146
  • 147
0

java xxx.java : assumes all the required classes are in its classpath or part of standard java library.

If your class require any extra jar or class then you have to specify this in the classpath as java -classpath .:xxx.jar Class name

If the referred classes are part of any directory then java -classpath dir xxx

Additionally, If the class having main method is placed in some package then use java -classpath dir pkg.xxx