permaculture playing cards*
The moose likes Beginning Java and the fly likes Regarding Package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding Package" Watch "Regarding Package" New topic
Author

Regarding Package

deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
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
sachin bhat
Greenhorn

Joined: May 13, 2011
Posts: 1
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
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and Welcome to JavaRanch sachin!
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
how to set classpath for this?
and i have the package name its now c:\example\model and c:\example\web
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

how did you compile your java file? just post the complete dos command with directory hierarchy!
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
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



Ewald Za Horn
Greenhorn

Joined: Aug 03, 2011
Posts: 5
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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and Welcome to JavaRanch Ewald Za Horn!
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
getting the same error once again...Is there any permanent solution for this.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
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
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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?
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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!
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
THANKS A LOT MILLIONS TIMES.FINALLY
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

deepak carter wrote:THANKS A LOT MILLIONS TIMES.FINALLY

you are welcome!
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
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?
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
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
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Start a new thread for a new question! It will help you to get more responses.
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Package
 
Similar Threads
Compiling via Command prompt.
problem with using packages
Error: Cannot find symbol during Importing package
Command line arguments
Error in Package