Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issues with Packages

 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, thanks for the explanation. :-D

Michael
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic