| Author |
Compiling a class which is dependent on other class
|
cle tan
Ranch Hand
Joined: Jun 11, 2012
Posts: 68
|
|
I have 2 classes
UseMe.java
ImplementClass.java
Both are located in C:/xampp/tomcat/webapps/proj1/WEB-INF/classes/test
i opened both in notepad ++, click run and click open current dir command
then i typed
javac UseMe.java ImplementClass.java
it compiled the classes
then when i tried to type:
C:\xampp\tomcat\webapps\proj1\WEB-INF\classes\test>java ImplementClass.java
Error: Could not find or load main class ImplementClass.java
C:\xampp\tomcat\webapps\proj1\WEB-INF\classes\test>java ImplementClass
Exception in thread "main" java.lang.NoClassDefFoundError: ImplementClass (wrong
name: test/ImplementClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472)
I have run the main method in Netbeans and it works
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1409
|
|
|
Try dropping the .java extension when invoking the java executable.
|
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
|
 |
cle tan
Ranch Hand
Joined: Jun 11, 2012
Posts: 68
|
|
Jelle Klap wrote:Try dropping the .java extension when invoking the java executable.
it gives me many exceptions
what does this mean
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Instead of this....
C:\xampp\tomcat\webapps\proj1\WEB-INF\classes\test>java ImplementClass
Do this...
C:\xampp\tomcat\webapps\proj1\WEB-INF\classes>java test.ImplementClass
meaning go up one directory -- to the class root. And specify the fully qualified class name.
And of course, this assumes that you haven't set a classpath variable.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1409
|
|
|
Any number of things, depending on which exceptions are thrown...
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
cle tan wrote:
I have run the main method in Netbeans and it works
That is because Netbeans does a lot of things for you -- among them setting up the correct classpaths, executing the program correctly, etc..... This is why it is recommended that beginners don't start with an IDE -- as it sometimes prevents learning the basics.
Henry
|
 |
cle tan
Ranch Hand
Joined: Jun 11, 2012
Posts: 68
|
|
Henry Wong wrote:
Instead of this....
C:\xampp\tomcat\webapps\proj1\WEB-INF\classes\test>java ImplementClass
Do this...
C:\xampp\tomcat\webapps\proj1\WEB-INF\classes>java test.ImplementClass
meaning go up one directory -- to the class root. And specify the fully qualified class name.
And of course, this assumes that you haven't set a classpath variable.
Henry
ok it works now
but what if i create a new class which is dependent on ImplementClass.java and UseMe.java
Do I have to recompile them again?
javac ImplementClass.java UseMe.java NewClass.java
|
 |
Bala Gangadhar
Ranch Hand
Joined: Oct 07, 2008
Posts: 66
|
|
|
No need to compile them again, unless you modified those classes.
|
 |
cle tan
Ranch Hand
Joined: Jun 11, 2012
Posts: 68
|
|
Bala Gangadhar wrote:No need to compile them again, unless you modified those classes.
because i'm afraid if i type
javac Newclass.java
it doesn't recognise the methods that i took from UseMe.java and ImplementClass.java
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
cle tan wrote:ok it works now
but what if i create a new class which is dependent on ImplementClass.java and UseMe.java
Do I have to recompile them again?
javac ImplementClass.java UseMe.java NewClass.java
The compiler is smart enough to automatically compile dependent java files (if it needs to) -- so all you need to do is compile the new java file.
However, since you are using packages, and not using classpaths, it may be a good idea to compile from the class root directory. Meaning go up one directory, and do this...
javac test\NewClass.java
Henry
|
 |
 |
|
|
subject: Compiling a class which is dependent on other class
|
|
|