• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issues with Packages

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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






 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"...
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, thanks for the explanation. :-D

Michael
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic