wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How do I import a class in another servlet class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I import a class in another servlet class?" Watch "How do I import a class in another servlet class?" New topic
Author

How do I import a class in another servlet class?

Will Clancey
Greenhorn

Joined: Dec 04, 2013
Posts: 2
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!




Ivan Marik
Greenhorn

Joined: Nov 04, 2013
Posts: 7
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

Joined: Dec 04, 2013
Posts: 2
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I import a class in another servlet class?