I have this simple class, compiled and ready in a .class file:
$ cat CensusData.java
package cg;
public class CensusData
{
public int population;
public int houses;
public int returns;
public int agi_avg;
public int refunds;
public int refund_avg;
}
and a desirous user of it (with irrelevancies elided)
$ head CountyAgi.java
import cg.CensusData;
public class CountyAgi extends Configured implements Tool
{
public static class Map
extends Mapper<LongWritable, Text, Text, CensusData>
{
private CensusData cd = new cg.CensusData();
But compiling this claims that
package cg does not exist
and
symbol CensusData does not exist
Everything being in the same directory, I thought the package was not
even necessary and originally did not have it. I added it just to try
to get it to compile, with no good effect. The current configuration
seems to match what the tutorial at oracle.com shows. I think perhaps
my problem is in the compiler invocation rather than the program
structure.
javac -classpath /usr/lib/hadoop/hadoop-core-0.20.2+320.jar:/usr/lib/
hadoop/hadoop-mapred-0.20.2+320.jar:/usr/lib/hadoop/hadoop-
hdfd-0.20.2+320.jar -d . CountyAgi.java
I've tried adding CensusData.class to the command line, and putting
CensusData in a jar and adding that, but nothing works.
Can you help a neophyte?
Thank you.
>I think perhaps
> my problem is in the compiler invocation rather than the program
> structure.
Program structure and command line arguments must agree or stuff is not
going to work. So we'll need to see how your files are laid out on disk
too. Please include the output of "pwd" as well as a directory listing
showing the location of these two files relative to where you are
invoking the compiler. Then invoke the compiler and show us the full
invocation line (again) and its output.
>
> javac -classpath /usr/lib/hadoop/hadoop-core-0.20.2+320.jar:/usr/lib/
> hadoop/hadoop-mapred-0.20.2+320.jar:/usr/lib/hadoop/hadoop-
> hdfd-0.20.2+320.jar -d . CountyAgi.java
^^^^^^^^^^^^^^
This is suspicious. I don't often work directly with the command line
tools but I seem to remember specifying the path to the .java files,
including the package name(s), or else compiling everything from a root
directory, which again includes the package names. Check carefully the
directory layouts and make sure the file paths to the .java files
contain the package names exactly like the tutorial shows.
Oh, and can you link to which tutorial you are using? There's more than
one...
But 'CountyAgi' does not have a 'package' directive! The only way that
classes don't need fully-qualified names (FQNs) or equivalent 'import'
directives is if they're in the same package.
> even necessary and originally did not have it. I added it just to try
> to get it to compile, with no good effect. The current configuration
> seems to match what the tutorial at oracle.com shows. I think perhaps
> my problem is in the compiler invocation rather than the program
> structure.
No, the program structure is wrong, too.
]
> javac -classpath /usr/lib/hadoop/hadoop-core-0.20.2+320.jar:/usr/lib/
> hadoop/hadoop-mapred-0.20.2+320.jar:/usr/lib/hadoop/hadoop-
> hdfd-0.20.2+320.jar -d . CountyAgi.java
You omitted "." from the classpath and did not include the directory for
'CountyAgi.java'.
> I've tried adding CensusData.class to the command line, and putting
What do you mean you added "CensusData.class to the command line"?
Which command line, the "javac" command? You never specify class files to
"javac" or "java", you specify classes and classpaths.
> CensusData in a jar and adding that, but nothing works.
>
> Can you help a neophyte?
javac \
-classpath
/usr/lib/hadoop/hadoop-core-0.20.2+320.jar:/usr/lib/hadoop/hadoop-mapred-0.20.2+320.jar:/usr/lib/hadoop/hadoop-hdfd-0.20.2+320.jar:.
\
-d . cg/*.java
--
Lew
It's in both.
markspace wrote:
> Program structure and command line arguments must agree or stuff is not
> going to work. So we'll need to see how your files are laid out on disk
> too. Please include the output of "pwd" as well as a directory listing
> showing the location of these two files relative to where you are
> invoking the compiler. Then invoke the compiler and show us the full
> invocation line (again) and its output.
...
> Oh, and can you link to which tutorial you are using? There's more than
> one...
OP, use this one:
<http://download.oracle.com/javase/tutorial/java/package/index.html>
--
Lew
Well, if I may ask one question: what is the purpose of you setting the
classpath in such a strange manner? Also note which directory is *not*
in the classpath.
By and large, if you can't explain what an option is doing in the javac
or java command line, you probably shouldn't be using it.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
> On 09/06/2010 02:57 PM, Clarence wrote:
>
>> javac -classpath /usr/lib/hadoop/hadoop-core-0.20.2+320.jar:/usr/lib/
>> hadoop/hadoop-mapred-0.20.2+320.jar:/usr/lib/hadoop/hadoop-
>> hdfd-0.20.2+320.jar -d . CountyAgi.java
>
> Well, if I may ask one question: what is the purpose of you setting the
> classpath in such a strange manner?
What's strange about it?
> Also note which directory is *not* in the classpath.
>
> By and large, if you can't explain what an option is doing in the javac
> or java command line, you probably shouldn't be using it.
You should find the guy at JBoss who is responsible for -server being in
their run.conf and tell him that!
tom
--
Thinking about it, history begins now -- sarah
That his classpath has a library he doesn't appear to need?
> On 09/06/2010 04:17 PM, Tom Anderson wrote:
>> On Mon, 6 Sep 2010, Joshua Cranmer wrote:
>>
>>> On 09/06/2010 02:57 PM, Clarence wrote:
>>>
>>>> javac -classpath /usr/lib/hadoop/hadoop-core-0.20.2+320.jar:/usr/lib/
>>>> hadoop/hadoop-mapred-0.20.2+320.jar:/usr/lib/hadoop/hadoop-
>>>> hdfd-0.20.2+320.jar -d . CountyAgi.java
>>>
>>> Well, if I may ask one question: what is the purpose of you setting
>>> the classpath in such a strange manner?
>>
>> What's strange about it?
>
> That his classpath has a library he doesn't appear to need?
CountyAgi needs it. What he showed us was "with irrelevancies elided", and
the fact that the class extends Configured and implements Tool makes me
think it's using at least these:
http://hadoop.apache.org/common/docs/current/api/org/apache/hadoop/conf/Configured.html
http://hadoop.apache.org/common/docs/current/api/org/apache/hadoop/util/Tool.html
Thank you all for your responses. The tutorial I was referring to is
at http://download.oracle.com/javase/tutorial/java/index.html
I found the part I was missing in the "Managing Source and Class
files" section. I see that either
1) my original setup (with no package declarations and CountyAgi and
CensusData in the same directory I was compiling from) would have
worked had I but included "." in the classpath
2) having put CensusData in a named package, I then had to also put
it in a subdirectory of that same name.
Tom is correct that this is a Hadooop program, and Joshua is correct,
for a different reason, that there are more items in the classpath
than are needed, because it would appear that some Hadoop libraries
have been consolidated since the docs were written.
Glad you found that link helpful.
> 1) my original setup (with no package declarations and CountyAgi and
> CensusData in the same directory I was compiling from) would have
> worked had I but included "." in the classpath
It is not recommended to use the so-called "default package" (no package) for
real code, though it's harmless enough in quick-and-dirty experiments.
> 2) having put CensusData in a named package, I then had to also put
> it in a subdirectory of that same name.
When I first learned Java in the '90s I found the link between packages and
directories very tricky to grok. To this day I still have to re-read the
directions and experiment to be certain I have the syntax right for the
"javac" command.
> Tom is correct that this is a Hadooop program, and Joshua is correct,
> for a different reason, that there are more items in the classpath
> than are needed, because it would appear that some Hadoop libraries
> have been consolidated since the docs were written.
Now that you're on the road to mastering packages you are well on your way to
mastering Java.
For further education there are at least two books you should own and a number
of online resources that will assist:
Books:
/Effective Java/ by Joshua Bloch.
http://java.sun.com/docs/books/effective/
/Java Concurrency in Practice/ by Brian Goetz, et al.
http://www.javaconcurrencyinpractice.com/
Anything by Doug Lea or the above two.
Online resources:
https://www.ibm.com/developerworks/java/
Brian Goetz is a regular contributor to Developerworks.
http://mindprod.com/jgloss/jgloss.html
Roedy Green has put together a compendium of useful Java-related information.
Much of it is specifically geared to the newcomer.
and more.
--
Lew
>As always with a new system, I seem to have more trouble with the
>needed infrastructure rather than the expression of my desires in
>code. With Java, I am unable to get one class to use another.
see:
http://mindprod.com/jgloss/import.html
http://mindprod.com/jgloss/package.html
http://mindprod.com/jgloss/classpath.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.