The servlet needs to access a method in the normal java file (which is in the same package), although I cannot seem to get this to work no matter what I try.
Here is the current setup;
Other Java File:
The 'Other Java file' compiles ok, but when I try and declare a new instance of this in the servlet, it throws an error of "package does not exist".
I have followed information in books about how to do this and tutorials online, although it is just not working. Can anyone see something obvious that I am missing here?
The full directory structure is as follows;
--.java files & jsp files
you may really mean to have
Also, to compile, just navigate to the classes directory (don't go into mypackage) and drop the -d parameter.
If your source files are in the mypackage dir (they should be) then enter the following:
There are numerous ways to do this but that should get you going I hope.
To compile both .java files I navigate to the correct folder in command prompt (Tomcat Root/mypackage) then ...
You shouldn't put your source code inside Any application server instead have a different place for your development code. Something like "/home/<username>/Dev/.... Then once you compiled and packaged you may move the final product in to the Tomcat (or any other server).
And you seems to have put all the things directly inside Tomcat root (ROOT)? Then have your own context ( or application folder) like "MyTestApp"...
Ed Ward wrote:If your source files are in the mypackage dir (they should be)
No, as already pointed out, they should not be part of the web applciation. Build them elsewhere and place only class files in the /classes hierarchy.
Then with regards to compiling the different way, that has worked. Why was the previous way I was doing it not working? My understanding of the command that I was using was that it is just a normal compile, but it places the .class files automatically in their directories?
@Vijitha Kumara, I have placed all of my files inside my package, which is inside the Tomcat Root. This is just on my local machine and not anywhere live, although I should get into the good practice of placing things in their correct places :-)
Thanks everyone for the help in such a fast time, it is much appreciated.
My understanding of the command that I was using was that it is just a normal compile, but it places the .class files automatically in their directories?
When you compile the second source file inside the package directory javac would not be able to find the other class file which is required in order for a successful compile, as it start looking a directory named "mypackage" inside "mypackage" unless you specify classpath option which points to the place where you have the class file.