wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Error In package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error In package" Watch "Error In package" New topic
Author

Error In package

Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
hey
i am new in servlets.
i am creating a servlet called test.java at location D:\project\

test.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import foo.*;

public class test extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)

throws IOException,ServletException
{
res.setContentType("text/html");
String username=req.getParameter("uname");
req.setAttribute("name",username);
person p=new person();
p.setname(username);
RequestDispatcher view=req.getRequestDispatcher("1.jsp");
view.forward(req,res);
}
}

i hava a folder at d:\project\WEB-INF\classes\ foo
in foo i made a class called person.java

person.java

package foo;

public class person
{
String name;
public String setname(String name)
{
this.name=name;
return name;
}
}

i have set classpath as C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar


now the problem is that i can successfully compile person.java

but i cant compile test.java . when i am compiling it there is an error that package foo does not exit. n cannot find symbol person.
please help anyone.
thanhx in advance.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1041
    
  13

What is the exact command line that you are using for compiling?

Forget about using the classpath environment variable; it just causes more problems than it solves. Instead, use the -classpath option of javac.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39832
    
  28
Welcome to the Ranch
Agree with Jk Robbins. If you wrote the system classpath yourself, delete it. If there was anything in there before you added that classpath, add .; to start the classpath and delete the entry you wrote. Start reading about classpaths here.
Please always indent your code and use the code button; it makes it much nicer to read.
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
this is the whole problem. please solve this...
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
why i am not able to add the screenshot of my problem??


[Thumbnail for Capture.PNG]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39832
    
  28
Please copy and paste the error message; screenshots are difficult to read. Please show us the directory structure, and what your classpath is at present.
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
DIrectory structure-->

project -> test.java, 1.jsp, web-inf
web-inf->classes
classes->foo
foo->person.java





this is the directory structure.....


commands inside cmd---

d: set classpath=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar

d:\project1> javac web-inf\classes\foo\person.java
it is compiling successfully

d:\project1>javac test.java
test.java:4 package foo doesnot exist in import foo.*
cannot find symbol person p=new person()
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33

D:\project1>set classpath=C:\Program Files\Apache Software Foundation\Tomcat 6.0
\lib\servlet-api.jar

D:\project1>javac web-inf\classes\foo\person.java

D:\project1>javac test.java
test.java:4: package foo does not exist
import foo.*;
^
test.java:13: cannot find symbol
symbol : class person
location: class test
person p=new person();
^
test.java:13: cannot find symbol
symbol : class person
location: class test
person p=new person();
^
3 errors

D:\project1>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39832
    
  28
You need the .; in the classpath still. Reset the classpath with .; at its start.
Why have you got the (incorrectly capitalised) person class in the web-inf folder? That is not in your classpath, so the javac tool cannot find it next time you compile anything.
You will probably find it quickest to drag all the .java files into the same folder, and compile them all with something like
javac -d . *.java
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
Thanhx sir..
My problem got solved by putting .; in classpath and by putting all the java files in the same folder.
But can you please explain why we put .; in the classpath and why all the java files are in same folder. what if i do not want to put all the classes in same folder? if all the classes are in same folder then how can we make the packages?? please explain sir..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39832
    
  28
How did you set that classpath?
There are several ways you can compile and create a package structure. You can put all your .java files in one folder and compile with the -d flag (you still need the . I showed earlier).
javac -d . *.java
The -d flag creates a directory structure and the . tells the compiler to start looking in the current directory. That works nicely until you have two files with the same name. Search my posts for “compile package name” and you should find old posts with more suggestions in .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error In package