aspose file tools*
The moose likes Servlets and the fly likes Issues with Packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Issues with Packages" Watch "Issues with Packages" New topic
Author

Issues with Packages

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
I have created a servlet & a normal java file.

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;

Servlet:


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?

Thanks
CKS






Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

Can you tell us how you compile it and how the directory structure is formed?

Tip on coding practices: Generally class names should start with a capital letter.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
To compile both .java files I navigate to the correct folder in command prompt (Tomcat Root/mypackage) then run this command, javac -d WEB-INF/classes homepage.java (or "otherjavafile.java").

The full directory structure is as follows;

Tomcat Root
-mypackage
--.java files & jsp files
--WEB-INF
---web.xml
---classes
----mypackage
-----homepage.class
-----otherjavafile.class
Ed Ward
Ranch Hand

Joined: Jan 30, 2006
Posts: 147
Well, just looking at the little code you've provided I suspect that where you have

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.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

Few things about your code:

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"...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
@Ed Ward, checked the first point and that was a typo on my part whilst posting the question.
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.

Regards

Michael
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

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.
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
I see, thanks for the explanation. :-D

Michael
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Issues with Packages