Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error In package

 
Sachin Kadian
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1725
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the whole problem. please solve this...
 
Sachin Kadian
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why i am not able to add the screenshot of my problem??
Capture.PNG
[Thumbnail for Capture.PNG]
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic