0

I have the following package structure where im trying to run a sample hibernate app

Source files: /home/chander/workspace/HibernateApp/src/com/hib/TranslateSQL.java

My current directory:

/home/chander/workspace/HibernateApp/src

Compile: $JAVA_HOME/bin/javac com/hib/*.java --- This ran successfully

Run: $JAVA_HOME/bin/java com/hib/TranslateSQL Giving the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: TranslateSQL (wrong name: com/hib/TranslateSQL)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:315)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: TranslateSQL.  Program will exit.

I have my classpath and JAVA_HOME set. I dont seem to find what is wrong with my command.

Also tried the following command:

$JAVA_HOME/bin/java com.hib.TranslateSQL

Got the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/hib/TranslateSQL
Caused by: java.lang.ClassNotFoundException: com.hib.TranslateSQL
    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:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:315)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: com.hib.TranslateSQL.  Program will exit.

2 Answers 2

2

You're specifying the classname incorrectly. Instead of this:

$JAVA_HOME/bin/java com/hib/TranslateSQL

you should be running:

$JAVA_HOME/bin/java com.hib.TranslateSQL

You should be specifying a class name which is within a package (com.hib), rather than a file name in a directory structure (com/hib.)

EDIT: If that's not working, your classpath may be set up incorrectly. You can specify it on the command line:

$JAVA_HOME/bin/java -cp . com.hib.TranslateSQL
Sign up to request clarification or add additional context in comments.

7 Comments

Already tried that. Getting the following exception: Exception in thread "main" java.lang.NoClassDefFoundError: com/hib/TranslateSQL Caused by: java.lang.ClassNotFoundException: com.hib.TranslateSQL at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method)
@ChanderShivdasani: It's always worth saying what you've tried to avoid us repeating that... will edit.
@ChanderShivdasani: Check the top of my edited answer - please clarify whether or not you're actually in the src directory; at the moment your javac wouldn't find the source file you mentioned.
Oops, i made a mistake. I am infact in src directory. I will edit my post!
Tried giving the classpath through command line as well, still getting the same exception.
|
0

I got it to work by doing this:

cd  /home/chander/workspace/HibernateApp/src/com/hib/

export CLASSPATH=$CLASSPATH:/home/chander/workspace/HibernateApp/src/

$JAVA_HOME/bin/java com.hib.TranslateSQL

1 Comment

That shouldn't have been necessary - the -cp . version in my answer should have been enough, unless the CLASSPATH also contained other things you needed, in which case the error message would have been different. You shouldn't need to be in the src/com/hib directory, either.

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.