aspose file tools*
The moose likes Beginning Java and the fly likes JDate not found in JDateTest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JDate not found in JDateTest" Watch "JDate not found in JDateTest" New topic
Author

JDate not found in JDateTest

Stephen Connery
Greenhorn

Joined: Jul 22, 2005
Posts: 7
Hi,
Iam sure this is an obvious problem but I have a class, JDateTest that tries to create a new JDate object but it cant seem to access the JDate class. Both JDate and JDateTest are both in the same folder.

I've tried "import com.javaranch.common.*;" but this still gives :

JDate not found in JDateTest

Heres the class, thanks:





public class JDateTest{


public static void main(String args[]){



JDate test = new JDate(2000,2,20);

System.out.println(test.get());


}//end main

}//end class JDateTest
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30548
    
152

Stephen,
Welcome to JavaRanch!

When you try to compile them, use:
javac *.java

The JDK will find them if both are compiled in the same shot.

[edited to fix typo]
[ July 22, 2005: Message edited by: Jeanne Boyarsky ]

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stephen Connery
Greenhorn

Joined: Jul 22, 2005
Posts: 7
Thanks Jeanne!
Do you mean like : javac JDate.java JDateTest.java ?
I tried this and got:
JDateTest.java cannot access JDate
bad class file: .\JDate.class
class contains wrong class: com.javaranch.common.JDate
please remove or make sure it appears in the correct subdirectory of the classpath

The JDate class compiles fine on its own.

I know such a simple error must seem daft -thanks for your help
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Stephen Connery:
Thanks Jeanne!
Do you mean like : javac JDate.java JDateTest.java ?
I tried this and got:
JDateTest.java cannot access JDate
bad class file: .\JDate.class
class contains wrong class: com.javaranch.common.JDate
please remove or make sure it appears in the correct subdirectory of the classpath

The JDate class compiles fine on its own.

I know such a simple error must seem daft -thanks for your help


I think Jeanne meant exactly what she said. You should type "javac *.java". Of course, I think the way you have done it should work, too. It sounds like the problem may be a directory and classpath issue. Can you please describe the directory structure that contains the two files for these classes? Which directory are you in when you try to compile? And have you set the CLASSPATH environment variable on your machine? If so, what is it?
Answers to these questions will help us figure out what you need to do to fix your problem.

Layne


Java API Documentation
The Java Tutorial
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
You need to include the JavaRanchCommon.jar (old) or jr.jar (current) which contains the JDate class in your classpath before you can import any classes from it.

The JDate is already compiled inside the jar. You don't need to recompile it.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Stephen Connery
Greenhorn

Joined: Jul 22, 2005
Posts: 7
Thank you Layne and Marilyn de Queiroz!

My (Path)classpath is set to:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Intel\Wireless\Bin\;c:\j2sdk1.4.1\bin

I got the JDate class from the jr.jar file.

I added c:\Jfiles\jr.jar to the path variable like:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Intel\Wireless\Bin\;c:\j2sdk1.4.1\bin;c:\Jfiles\jr.jar

And added : import com.javaranch.common.*; to the JDateTest class

Both classes JDate and JDateTest are in:

c:\Jfiles\code

This is where I compiled them from.

But still get the error:

JDateTest.java cannot access JDate
bad class file: .\JDate.class
class contains wrong class: com.javaranch.common.JDate
please remove or make sure it appears in the correct subdirectory of the classpath

Thanks for your help
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41900
    
  63

please remove or make sure it appears in the correct subdirectory of the classpath


This error message is right on target. You have the JDate class in your current directory, where it shouldn't be. It should be in a subdirectory called "com.javaranch.common", but actually, since you include the jr.jar file in your classpath, it shouldn't even be there. Just delete it, and things should be fine.


Ping & DNS - my free Android networking tools app
Stephen Connery
Greenhorn

Joined: Jul 22, 2005
Posts: 7
Thanks

I deleted JDate from the c:\Jfiles\code folder and it worked! Iam not too sure though why this worked. Did this work because the jr.jar file was included in the classpath or because the JDate class was in the c:\Jfiles\code\com\javaranch\common folder?

When you want to include a class where does that class have to be stored and is it different if the class is part of a package or in a jar file?


Cheers
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
You should do either one or the other. Either extract the class and put it in the c:\Jfiles\code\com\javaranch\common subdirectory or include the jar in your classpath. Not both.

By the way, I would recommend removing the c:\j2sdk1.4.1\bin from your CLASSPATH. Although it should be in your PATH, you should avoid putting any of your own code into that directory.
Stephen Connery
Greenhorn

Joined: Jul 22, 2005
Posts: 7
Cheers

So how does the compiler look for classes you wish to import? Can it only see classes in the current directory (and sub-directories) OR those included in the classpath.

Could you tell me whats the difference between the classpath and PATH variables

Thanks for your help!!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41900
    
  63
So how does the compiler look for classes you wish to import? Can it only see classes in the current directory (and sub-directories) OR those included in the classpath.

Could you tell me whats the difference between the classpath and PATH variables


The compiler will find class files that are in directories and jar/zip files in the CLASSPATH. It will also find source files that are in the current directory, or subdirectories of it, if the directory hierarchy corresponds exactly to the package hierarchy.
The interpreter ("java") will find everything in the classpath, but not by default anything that is in the current directory or its subdirectories; for that to work you need to include "." in the classpath.

CLASSPATH is only used by Java. PATH on the other hand is used by the shell to find executable files (.exe, .bat, .sh, ...) I wouldn't advise to share any directories between the two, simply because they have such very different purposes.
[ July 25, 2005: Message edited by: Ulf Dittmer ]
Stephen Connery
Greenhorn

Joined: Jul 22, 2005
Posts: 7
I found some stuff on this so if anyone else has the same question:

The Path variable:

The PATH variable is a list of directories separated by semi-colons. These directories are where your system looks for commands when you try to execute them. If you don't add the location of the SDK tools to your PATH, you'll have to specify where they are every time you want to use them.

The classpath helps JDK locate Java software (class files) that are installed on your machine.


Cheers
Stephen Connery
Greenhorn

Joined: Jul 22, 2005
Posts: 7
Many thanks Ulf Dittmer(and everyone else) for your response - I hadnt seen it before I posted - that helped to clear things up - cheers
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JDate not found in JDateTest