• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiling a class which is dependent on other class

 
cle tan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try dropping the .java extension when invoking the java executable.
 
cle tan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any number of things, depending on which exceptions are thrown...
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to compile them again, unless you modified those classes.
 
cle tan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic