• Post Reply Bookmark Topic Watch Topic
  • New Topic

Classes are in the same package but still cannot find each other so won't compile  RSS feed

 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






Above is the code for three classes. class Dog compiles fine but other two classes cannot find Dog.

Following is the classpath and the JAVA_HOME

classpath = .;C:\Program Files\Apache Software Foundation\Tomcat 8.0\lib\servlet-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 8.0\lib\jsp-api.jar

JAVA_HOME= C:\Program Files\Java\jdk1.8.0_25\bin;

I am using javac -d WEB-INF\classes WEB-INF\classes\demoServlet4\ListenerTest.java and javac -d WEB-INF\classes WEB-INF\classes\demoServlet4\MyServletContextListener.java to compile the classes.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not setting the sourcepath when compiling. Use the -sourcepath switch on the javac command and specify "WEB-INF\classes WEB-INF\classes\"
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help!!

I used -sourcepath and it worked.

Could you please tell me why we use -sourcepath? -d is for destination. Why do we use -sourcepath?
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so very much!

My servlet works fine now.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use javac to compile a file, it will find that file regardless of what the sourcepath or classpath is, because you specified its location explicitly. However, classes referenced from there are not located relative to the file you're trying to compile, but relative to the classpath and sourcepath.

The classpath is used to locate binaries, and sourcepath to locate source code. Initially, the sourcepath is set to the current working directory, so if you don't specify it, javac will try to locate your Dog class at MyProject\demoServlet4\Dog.java, and not MyProject\WEB-INF\classes WEB-INF\classes\demoServlet4\Dog.java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!