• 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

How do I import a class in another servlet class?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, everyone

I am a java newbie and I basically have a very simple class like:

public class RequestClass {

private String username;

private String pass;

}

I have compiled it and I want to import it in another class called LoginController. The class RequestClass is basically in the same folder and right next to LoginController (so the same package, so it shouldn't need to be imported). But if I try not to import it in the LoginController class, then it simply doesn't find the RequestClass and when compiling the LoginController says error: cannot find symbol.

This is the LoginController class:




The purpose of this whole class as written here is for testing only! I am basically passing a username and password with javascript using the JSON.stringify method. I am deserializing the object using the com.google.gson library.I am then converting the object right away to a json object to be sent back again to the front-end. I know, I know this is probably not the best way to test things but everything should be working because I followed the official documentation for using the com.google.gson library and a bunch of tutorials. I am getting the ridiculous and very newbie error: '.' expected import RequestClass;

I am also guessing that this error is so ridiculous that a lot of you haven't even seen it before. So obviously the RequestClass isn't imported correctly and I just don't know how to import it in this case. I have programmed in many other languages but not that much in Java.


I know I am missing some very basic concepts here. Please, help me!




 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using an IDE and if so did you import the class library using the IDE or just put it in the folder. I've gotten similar errors before Doing the same thing.
 
Will Clancey
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ivan Marik wrote:Are you using an IDE and if so did you import the class library using the IDE or just put it in the folder. I've gotten similar errors before Doing the same thing.



No, I am not using an IDE precisely because I don't want to do anything that I don't understand completely. I compile first the RequestClass and then the LoginController. They are both in the same folder and yet when I compile the LoginController.java it cannot find the RequestClass.class unless I import it but then there are the other errors. I am guessing the problem lies in that the LoginController is actually a servlet! Because if I want to compile another class that uses RequestClass and is not a servlet then there is not problem.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not need to import classes that are in the same package (in fact, it's not even possible) - you shouldn't need the import statement at all.

If the compiler tells you it can't find the class, then that is because the current directory "." isn't in your classpath. Make sure it's included in your classpath when you compile your code. For example:

javac -cp .;C:\Somewhere\servlet-api.jar;C:\SomewhereElse\some-library.jar LoginController.java

Note: Your own code is not in any package. That's not recommended. It's better to learn how to use packages and organize your own code better by putting your own classes in a package.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic