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

2
  • 1
    does the class Test belong to a specific package? Any package statement in Test.java? Commented Oct 5, 2012 at 15:05
  • No. Test does not belong to any package. Commented Oct 5, 2012 at 15:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.