Forums Register Login

Regarding Package

+Pie Number of slices to send: Send
Hi

we have created two packages

1.c:\java\model
2.c:\java\web

in web package i have created a file called Test1.java having source code



in model package i have created a file called Test2.java having source code



when i am running this file its showing me this error..




Tool completed with exit code 1



Can anyone please help me in this....

Thanks in advance
1
+Pie Number of slices to send: Send
hi deepak,
The package name cannot begin with java as it is reserved by sunMicrosystem(Oracle).

please refer this link for more info

http://java.sun.com/docs/books/jls/third_edition/html/names.html
+Pie Number of slices to send: Send
you should get this error while compiling! did you set classpath ? how did you compile? once you get compilation successful then you can expect an exception [see sachin post]
+Pie Number of slices to send: Send
and Welcome to JavaRanch sachin!
+Pie Number of slices to send: Send
how to set classpath for this?
and i have the package name its now c:\example\model and c:\example\web
+Pie Number of slices to send: Send
how did you compile your java file? just post the complete dos command with directory hierarchy!
+Pie Number of slices to send: Send
First of all thank you for helping me out
I have following directly structure

c:\ranch\model with file Test2.java

and

c:\ranch\web with file Test1.java

and running like this...

c:\ranch\model>javac Test2.java and its showing this error

C:\ranch\model\Test2.java:2: package ranch.web does not exist
import ranch.web.*;
^
C:\ranch\model\Test2.java:12: cannot find symbol
symbol : class Test1
location: class ranch.model.Test2
Test1 test=new Test1();
^
C:\ranch\model\Test2.java:12: cannot find symbol
symbol : class Test1
location: class ranch.model.Test2
Test1 test=new Test1();
^
3 errors

Tool completed with exit code 1


The SOURCE CODE FOR THE FILE Test2 is
package ranch.model;
import ranch.web.*;

public class Test2
{
//Test1 test=new Test1();
//test.run();

public static void main(String[] args)
{
//Test2 test2=new Test2();
Test1 test=new Test1();
test.run();

}
}


the source code for file Test1 is

package ranch.web;

public class Test1
{
public void run()
{
System.out.println("???");
}
}



Thanks in advance



+Pie Number of slices to send: Send
Hi.

I can compile everything by running the following from the command prompt (example for Test2.java):

c:>javac ranch\model\Test2


To be able to RUN the file, I need to make sure Java knows where to find the classes I'm importing, in this case, the ones in the web package. You can think of packages like folders or directories.

You have the following structure:

ranch->web
ranch->model

If you are IN the ranch.web package, it has no way of figuring out where the ranch.model package is, as it's not visible at the moment. The simplest solution is to run the command in such a way that it's easy to find the folder, for example:

C:>java ranch.model.Test2

This will generate the output correctly as it makes it easy for Java to find the classes you are important relative to the current folder. An alternative would of course be to update the CLASSPATH to include your own classes.

Might I suggest you Google Java Classpath and learn how that works? Packages can be a bit confusing in the beginning, but you'll soon get the hang of it.
+Pie Number of slices to send: Send
and Welcome to JavaRanch Ewald Za Horn!
+Pie Number of slices to send: Send
getting the same error once again...Is there any permanent solution for this.
+Pie Number of slices to send: Send
 

deepak carter wrote:getting the same error once again...Is there any permanent solution for this.


did you follow the ewald post?
compile as in c:>javac ranch\model\Test2.java
+Pie Number of slices to send: Send
Hi,

Its compiling fine..Thanks everyone for that....one step way ...its showing error while running.....

i am following ewald recommendations
c:\java ranch\model\Test2


showing a huge list of error
+Pie Number of slices to send: Send
 

deepak carter wrote:
i am following ewald recommendations
c:\java ranch\model\Test2


Not really! C:>java ranch.model.Test2 it is different from your command right?
+Pie Number of slices to send: Send
Hi
with command
i.e C:\javac ranch\mode\Test2.java

i am able to compile fine but not able to run.



i really need to end this.
+Pie Number of slices to send: Send
 

deepak carter wrote:
i.e C:\javac ranch\mode\Test2.java


javac uses \ to seperate the packages and *java uses . to seperate the packages*. so
when you run the class, please use . as in java ranch.model.Test2 . hope your Test2.class is in ranch.model package!
+Pie Number of slices to send: Send
THANKS A LOT MILLIONS TIMES.FINALLY
+Pie Number of slices to send: Send
 

deepak carter wrote:THANKS A LOT MILLIONS TIMES.FINALLY


you are welcome!
+Pie Number of slices to send: Send
Rather than start a new thread, I seem to have the same compilation problem in a slightly more complex situation.

The following works:


Directory structure:
C:\Java\HF\bV1\src\com\example\model
C:\Java\HF\bV1\src\com\example\web

From C:\Java\HF\bV1\src:

javac com\example\model\BE.java
javac com\example\web\BS.java
java com.example.model.BE

And this works:

From C:\Java\HF\bV1:

javac -classpath "C:\Program Files\Apache Software Foundation\jakarta-tomcat-5.0.28\common\lib\servlet-api.jar";classes:. -d classes src\com\example\model\BE.java

But this does not, yielding "package com.example.model does not exist" (and note that without the import statement it DOES work):

javac -classpath "C:\Program Files\Apache Software Foundation\jakarta-tomcat-5.0.28\common\lib\servlet-api.jar";classes:. -d classes src\com\example\web\BS.java

What am I doing wrong?
+Pie Number of slices to send: Send
To add to that...
I a m running example from Kathy 's Servlet and JSP book....chapter 3...


i have following directory structure.

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\model\BeerExpert.java

and

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.java

The source code for BeerSelect is
package com.example.web;
import com.example.model.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class BeerSelect extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
String c=req.getParameter("color");
BeerExpert be=new BeerExpert();
List result=be.getBrands(c);
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("Beer Selection Advice<br>");
Iterator it=result.iterator();
while(it.hasNext())
{
out.println("<br>try: "+it.next());
}
}
}

and source code for BeerExpert is

package com.example.model;
import java.util.*;

public class BeerExpert
{
public List getBrands(String color)
{
List brands=new ArrayList();
if(color.equals("amber"))
{
brands.add("Jack Amber");
brands.add("Red Moose");
}
else
{
brands.add("Jail Pale Ale");
brands.add("Gout Stout");
}
return(brands);
}
}


Beer Expert is compiling fine but whenevr i am running BeerSelect its giving me an error


C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.java:2: package com.example.model does not exist
import com.example.model.*;
^
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.java:14: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be=new BeerExpert();
^
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.java:14: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be=new BeerExpert();
^
3 errors

Tool completed with exit code 1


Can anyone help in this regard


Thanks in advance
+Pie Number of slices to send: Send
Start a new thread for a new question! It will help you to get more responses.
+Pie Number of slices to send: Send
Hmm... Sorry, I guess I don't understand the local customs. On many forums it's considered spam to start new threads on what appears to be the same issue. If there are no responses I'll start a new thread.

By the way, I am working on the same walkthrough as Deepak from Head First Servlets and JSPs. I can get the sample to run if I compile locally and then manually deploy the resulting class file, but it doesn't appear that should be necessary.

Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1964 times.
Similar Threads
problem with using packages
Error: Cannot find symbol during Importing package
Command line arguments
Error in Package
Compiling via Command prompt.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 03:50:43.