Home arrow static arrow Java Programming [Archive] - Help! With Packages...I'm new & frustrated!
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Help! With Packages...I'm new & frustrated!
This topic has 13 replies on 1 page.

Posts:22
Registered: 9/11/00
Help! With Packages...I'm new & frustrated!  
Jun 22, 2004 10:05 AM



 
In the following directory structure, I have 2 classes, a DBHandler class that hits a database and a GetData class uses the DBHandler class to get specific data. I want to put both of these into a WebSiteClasses package. When I try to package both of them to the WebSiteClasses package, the DBHandler class compiles just fine. But when I try to compile the GetData class (that uses the DBHandler class) I get an error stating the following:
"cannot resolve symbol
symbol : class DBHandler".

My code for the two java files is below this directory structure. I'm rather new at this and any help would be greatly appreciated


- tomcat
- bin
- classes
        - WebSiteClasses
                DBHandler.class
                DBHandler.java
                GetData.class
                GetData.java
- common
- conf
- lib
- logs
- server
- temp
- work
=====================================================
JAVA Files ------>>>>
DBHandler.java:

package WebSiteClasses;
import java.sql.*;

public class DBHandler{

public ResultSet getData(String sql){
Connection cn = null;
Statement st = null;
ResultSet rs = null;

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:FileToFile";
String user = "";
String pwd = "";
cn = DriverManager.getConnection(url,user,pwd);
st = cn.createStatement();
rs = st.executeQuery(sql);

}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return rs;
}
}


GetData.java:

package WebSiteClasses;
import java.sql.*;
import java.io.*;
import java.util.ArrayList;

public class GetData{
DBHandler db = new DBHandler();
String[] monthList;

public GetData() throws Exception{
getTrans();
}

public ArrayList getTrans() throws Exception{
ResultSet rs = db.getData("select distinct Transaction from tblTransactReport");
ArrayList list = new ArrayList();
try{
while (rs.next()){
String Transaction = rs.getString("Transaction");
list.add(Transaction);
}
}catch (SQLException e){e.printStackTrace();}
return list;
}
}

 

Posts:37,103
Registered: 3/30/99
Re: Help! With Packages...I'm new & frustrated!  
Jun 22, 2004 10:13 AM (reply 1 of 13)



 
Compiles fine for me. Make sure you don't have old copies of either of the .class files or .java files laying around to confuse things. If it still doesn't work, paste the exact command you're using to compile it, as well as the directory structure where you are when you're excuting the compilation command. If you're using ant, paste your ant script.

When you post code, error messages, etc., please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read.
 

Posts:386
Registered: 11/2/01
Re: Help! With Packages...I'm new & frustrated!  
Jun 22, 2004 10:15 AM (reply 2 of 13)



 
good point jverd. I had the same thing happen to me. I finally just deleted the classes, recompiled and it worked out fine.
 

Posts:22
Registered: 9/11/00
Re: Help! With Packages...I'm new & frustrated!  
Jun 22, 2004 10:59 AM (reply 3 of 13)



 
Ok about the <code> </code> tags...I'll use them next time.

I've done both suggestions...deleting old class files (I did have some in other directories) and their directories, also deleted these classes and re-compiled them. I'm still getting the same error! I'm assuming that if it can compile one of these, then I have my environment variables set up correctly. This is totally baffling!

Dave
 

Posts:21,719
Registered: 98-02-20
Re: Help! With Packages...I'm new & frustrated!  
Jun 22, 2004 11:27 AM (reply 4 of 13)



 
Compiles fine for me. How are you doing it?

I saved your .java source files in the directory structure that you specified - a directory named classes, with WebSiteClasses as a directory under that, and the .java files in that directory.

I opened a command shell, navigated to the classes directory, and compiled using this command:

javac -d . WebSiteClasses\*.java


%
 

Posts:22
Registered: 9/11/00
Re: Help! With Packages...I'm new & frustrated!  
Jun 22, 2004 1:05 PM (reply 5 of 13)



 
Hey there, thanks for replying. Actually, I was using Textpad (CNTL+1) to compile, so I thought I'd do it your way...here are my error results in the DOS window (still no luck):

C:\tomcat\classes\WebSiteClasses>javac -d . WebSiteClasses\*.java
error: cannot read: WebSiteClasses\*.java
1 error

I'm actually getting this even when I try to compile the DBHandler class by itself (which compiles fine in Textpad):


C:\tomcat\classes\WebSiteClasses>javac -d . WebSiteClasses\DBHandler.java
error: cannot read: WebSiteClasses\DBHandler.java
1 error

I shouldn't have to reboot my pc after deleting old directories and classes, do I? It's amazing how all of you people can use these programs in a mirror directory and comile them just fine and yet I can't use my own!!! It almost seems like the package is not getting created.

Thanks again and I'm open for an other ideas you can think of!
Dave

 

Posts:9
Registered: 6/16/04
Re: Help! With Packages...I'm new & frustrated!  
Jun 22, 2004 1:38 PM (reply 6 of 13)



 
it appears that you are already in the directory where your .java files are, so you don't need to put in WebSiteClasses\*.java, you should just be able to do

C:\tomcat\classes\WebSiteClasses>javac -d *.java

but if that doesn't work, in your class that uses DBHandler.java, try adding the following:
import WebSiteClasses.DBHandler;
 

Posts:21,719
Registered: 98-02-20
Re: Help! With Packages...I'm new & frustrated!  
Jun 22, 2004 1:41 PM (reply 7 of 13)



 
Hey there, thanks for replying. Actually, I was using
Textpad (CNTL+1) to compile, so I thought I'd do it
your way...here are my error results in the DOS window
(still no luck):


C:\tomcat\classes\WebSiteClasses>javac -d .
WebSiteClasses\*.java
error: cannot read: WebSiteClasses\*.java
1 error

You didn't read my instructions closely enough. I said you should issue that command from the classes directory.

You shouldn't be compiling in Textpad or anywhere else unless you can do it perfectly every time on the command line.

I shouldn't have to reboot my pc after deleting old
directories and classes, do I? It's amazing how all
of you people can use these programs in a mirror
directory and comile them just fine and yet I can't
use my own!!! It almost seems like the package is not
getting created.

Uh, no, you shouldn't have to reboot.

You should go back and read about how javac.exe and packages work.

%

 

Posts:21,719
Registered: 98-02-20
Re: Help! With Packages...I'm new & frustrated!  
Jun 22, 2004 1:43 PM (reply 8 of 13)



 
it appears that you are already in the directory where
your .java files are, so you don't need to put in
WebSiteClasses\*.java, you should just be able to do

C:\tomcat\classes\WebSiteClasses>javac -d *.java

Don't need the -d in this case.

but if that doesn't work, in your class that uses
DBHandler.java, try adding the following:
import WebSiteClasses.DBHandler;

No, you don't have to import DBHandler into GetData, because they're already in the same package.

%
 

Posts:22
Registered: 9/11/00
Re: Help! With Packages...I'm new & frustrated!  
Jun 23, 2004 5:56 AM (reply 9 of 13)



 
I got it to work and thanks for your help. However, call me stupid, but I still don't quite understand this whole concept. You mentioned to read more about how javac.exe and packages work. Do you have any COMPREHENSIVE references? Everywhere I've read about packages, in books or on line tutorials, merely states the obvious...create a folder and then name that folder in a package statement in the JAVA program.

Thanks.
 

Posts:21,719
Registered: 98-02-20
Re: Help! With Packages...I'm new & frustrated!  
Jun 23, 2004 6:16 AM (reply 10 of 13)



 
"...Everywhere I've read about packages, in books or on line tutorials, merely states the obvious...create a folder and then name that folder in a package statement in the JAVA program..."

Not so obvious, apparently.

I don't know about a comprehensive reference - explaining CLASSPATH shouldn't take a 1,000 page book.

I think it's important to understand the connection between the package names, the directory structure, the fully-resolved class name, and the way that javac.exe and java.exe go looking for classes. Maybe you need to look past the obvious and think about this for a while longer.

If you have a class X with package a.b.c, do you understand what the directory structure must look like? Where should the X.class file be? If you're using a command shell, what directory should you navigate to in order to compile X.java, and where should X.class end up when you're finished? When you try to run your new X.class, how do you invoke it in java.exe?

If you can answer all those, you're in better shape.

%

[url=http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/classpath.html]How To Set The CLASSPATH[/url]
 

Posts:21,719
Registered: 98-02-20
Re: Help! With Packages...I'm new & frustrated!  
Jun 23, 2004 6:17 AM (reply 11 of 13)



 
The key thing is understanding how the class loader finds the .class files it needs when you run Java.

%
 

Posts:22
Registered: 9/11/00
Re: Help! With Packages...I'm new & frustrated!  
Jun 23, 2004 9:42 AM (reply 12 of 13)



 
Thanks for the link...I'm starting to understand it better. Even put my stuff into a servlet and it seems to be working.

Thanks for the help!
 

Posts:21,719
Registered: 98-02-20
Re: Help! With Packages...I'm new & frustrated!  
Jun 23, 2004 10:15 AM (reply 13 of 13)



 
CLASSPATH is the #1 problem for most Java newcomers. (Search the forum and see.)

Check this link, too:

http://wiki.java.net/bin/view/Javapedia/ClassPath

%
 
This topic has 13 replies on 1 page.