Why Doesn't my Code Work with this Jar File?

K

KevinSimonson

I'm having a nasty time figuring out how to use JAR files. I have a
directory "Jrs" that includes an "abc" directory and a "def"
directory. In directory "abc" I have a single file called
"AbcCl.java" coded:

package abc;
public class AbcCl
{
public String toString ()
{
return "[AbcCl Object]";
}
}

and in directory "def" I have a single file called "DefCl.java" coded:

package def;
public class DefCl
{
public String toString ()
{
return "[DefCl Object]";
}
}

Back in "Jrs" I have a file "UseEm.java" coded:

import abc.AbcCl;
import def.DefCl;

public class UseEm
{
public static void main ( Strin[] arguments)
{
AbcCl ac = new AbcCl();
DefCl dc = new DefCl();
System.out.println( "ac == " + ac + ", dc == " + dc + '.');
}
}

Then I executed "javac UseEm.java", which created three class files,
an "abc\AbcCl.class", a "def\DefCl.class", and a "UseEm.java".
Finally I created two jar files with commands "jar -cf abc.jar abc"
and "jar -cf def.jar def".

Then I created a "Jrs2" directory, a sibiling to "Jrs", and copied to
it "UseEm.java" and the two JAR files. In that "Jrs2" directory I
typed in the command "javac -cp abc.jar:def.jar", and as a result got
a whole bunch of error messages. I was kind of expecting that.

At that point I typed in "jar -xf abc.jar" which extracted the "abc"
directory, and I thought that therefore I should be able to compile
"UseEm.java" with the command "javac -cp .:def.jar UseEm.java". But
when I tried it I got error messages:

UseEm.java:1: package abc does not exist
import abc.AbcCl;
^
UseEm.java:2: package def does not exist
import def.DefCl;
^
UseEm.java:8: cannot find symbol
symbol : class AbcCl
location: class UseEm
AbcCl ac = new AbcCl();
^
UseEm.java:8: cannot find symbol
symbol : class AbcCl
location: class UseEm

AbcCl ac = new AbcCl();
^
UseEm.java:9: cannot find symbol
symbol : class DefCl
location: class UseEm
DefCl dc = new DefCl();
^
UseEm.java:9: cannot find symbol
symbol : class DefCl
location: class UseEm
DefCl dc = new DefCl();
^
6 errors

Does anybody have any idea what I'm doing wrong? Why can't I get the
code I need to compile out of this JAR file? And _why in the world_
can't my compiler see the <AbcCl> class? The "abc" directory is
_right there_, with an "AbcCl.class" and an "AbcCl.java" file right in
it?

Kevin Simonson
 
M

markspace

Does anybody have any idea what I'm doing wrong? Why can't I get the
code I need to compile out of this JAR file? Kevin Simonson


Dunno, your examples looked ok to me, at first glance anyway. The javac
man page has an example how to do what you are attempting. Take look at
the very end and try to follow their example. I think it would be
easier to get help if you could point out the difference in output from
that example.

<http://download.oracle.com/javase/7/docs/technotes/tools/solaris/javac.html>
 
A

Arved Sandstrom

I'm having a nasty time figuring out how to use JAR files. I have a
directory "Jrs" that includes an "abc" directory and a "def"
directory. In directory "abc" I have a single file called
"AbcCl.java" coded:

package abc;
public class AbcCl
{
public String toString ()
{
return "[AbcCl Object]";
}
}

and in directory "def" I have a single file called "DefCl.java" coded:

package def;
public class DefCl
{
public String toString ()
{
return "[DefCl Object]";
}
}

Back in "Jrs" I have a file "UseEm.java" coded:

import abc.AbcCl;
import def.DefCl;

public class UseEm
{
public static void main ( Strin[] arguments)
{
AbcCl ac = new AbcCl();
DefCl dc = new DefCl();
System.out.println( "ac == " + ac + ", dc == " + dc + '.');
}
}

Then I executed "javac UseEm.java", which created three class files,
an "abc\AbcCl.class", a "def\DefCl.class", and a "UseEm.java".
Finally I created two jar files with commands "jar -cf abc.jar abc"
and "jar -cf def.jar def".

Then I created a "Jrs2" directory, a sibiling to "Jrs", and copied to
it "UseEm.java" and the two JAR files. In that "Jrs2" directory I
typed in the command "javac -cp abc.jar:def.jar", and as a result got
a whole bunch of error messages. I was kind of expecting that.

At that point I typed in "jar -xf abc.jar" which extracted the "abc"
directory, and I thought that therefore I should be able to compile
"UseEm.java" with the command "javac -cp .:def.jar UseEm.java". But
when I tried it I got error messages:
[ SNIP ]
6 errors

Does anybody have any idea what I'm doing wrong? Why can't I get the
code I need to compile out of this JAR file? And _why in the world_
can't my compiler see the <AbcCl> class? The "abc" directory is
_right there_, with an "AbcCl.class" and an "AbcCl.java" file right in
it?

Kevin Simonson

With the exception of the manual typo in UseEm.java, the only thing
wrong with your procedure is that you should also have supplied '.' as
part of the classpath on your first javac attempt with the JARs
(otherwise javac doesn't know where UseEm.java is). But that absolutely
should have worked.

I know it should have because I just now did it. I always do just to
make sure there's nothing catching me out in a question. The only
difference is that I don't include *.java files in JARs.

Side note: 'jar' does not need a hyphen in front of its arguments, and
never has.

AHS
--
You should know the problem before you try to solve it.
Example: When my son was three he cried about a problem with his hand. I
kissed it several times and asked him about the problem. He peed on his
hand.
-- Radia Perlman, inventor of spanning tree protocol
 
L

Lew

Arved said:
KevinSimonson said:
I'm having a nasty time figuring out how to use JAR files. I have a
directory "Jrs" that includes an "abc" directory and a "def"
directory. In directory "abc" I have a single file called
"AbcCl.java" coded:

package abc;
public class AbcCl
{
public String toString ()
{
return "[AbcCl Object]";
}
}

and in directory "def" I have a single file called "DefCl.java" coded:

package def;
public class DefCl
{
public String toString ()
{
return "[DefCl Object]";
}
}

Back in "Jrs" I have a file "UseEm.java" coded:

import abc.AbcCl;
import def.DefCl;

public class UseEm
{
public static void main ( Strin[] arguments)
{
AbcCl ac = new AbcCl();
DefCl dc = new DefCl();
System.out.println( "ac == " + ac + ", dc == " + dc + '.');
}
}

Then I executed "javac UseEm.java", which created three class files,
an "abc\AbcCl.class", a "def\DefCl.class", and a "UseEm.java".
Finally I created two jar files with commands "jar -cf abc.jar abc"
and "jar -cf def.jar def".

Then I created a "Jrs2" directory, a sibiling to "Jrs", and copied to
it "UseEm.java" and the two JAR files. In that "Jrs2" directory I
typed in the command "javac -cp abc.jar:def.jar", and as a result got
a whole bunch of error messages. I was kind of expecting that.

At that point I typed in "jar -xf abc.jar" which extracted the "abc"
directory, and I thought that therefore I should be able to compile
"UseEm.java" with the command "javac -cp .:def.jar UseEm.java". But
when I tried it I got error messages:
[ SNIP ]
6 errors

Does anybody have any idea what I'm doing wrong? Why can't I get the
code I need to compile out of this JAR file? And _why in the world_
can't my compiler see the <AbcCl> class? The "abc" directory is
_right there_, with an "AbcCl.class" and an "AbcCl.java" file right in
it?

With the exception of the manual typo in UseEm.java, the only thing
wrong with your procedure is that you should also have supplied '.' as
part of the classpath on your first javac attempt with the JARs
(otherwise javac doesn't know where UseEm.java is). But that absolutely
should have worked.

I know it should have because I just now did it. I always do just to
make sure there's nothing catching me out in a question. The only
difference is that I don't include *.java files in JARs.

Side note: 'jar' does not need a hyphen in front of its arguments, and
never has.

As another side note, do not mix classes that are in packages with classes that are not.
 
A

Arne Vajhøj

Arved said:
KevinSimonson said:
I'm having a nasty time figuring out how to use JAR files. I have a
directory "Jrs" that includes an "abc" directory and a "def"
directory. In directory "abc" I have a single file called
"AbcCl.java" coded:

package abc;
public class AbcCl
{
public String toString ()
{
return "[AbcCl Object]";
}
}

and in directory "def" I have a single file called "DefCl.java" coded:

package def;
public class DefCl
{
public String toString ()
{
return "[DefCl Object]";
}
}

Back in "Jrs" I have a file "UseEm.java" coded:

import abc.AbcCl;
import def.DefCl;

public class UseEm
{
public static void main ( Strin[] arguments)
{
AbcCl ac = new AbcCl();
DefCl dc = new DefCl();
System.out.println( "ac == " + ac + ", dc == " + dc + '.');
}
}

Then I executed "javac UseEm.java", which created three class files,
an "abc\AbcCl.class", a "def\DefCl.class", and a "UseEm.java".
Finally I created two jar files with commands "jar -cf abc.jar abc"
and "jar -cf def.jar def".

Then I created a "Jrs2" directory, a sibiling to "Jrs", and copied to
it "UseEm.java" and the two JAR files. In that "Jrs2" directory I
typed in the command "javac -cp abc.jar:def.jar", and as a result got
a whole bunch of error messages. I was kind of expecting that.

At that point I typed in "jar -xf abc.jar" which extracted the "abc"
directory, and I thought that therefore I should be able to compile
"UseEm.java" with the command "javac -cp .:def.jar UseEm.java". But
when I tried it I got error messages:
[ SNIP ]
6 errors

Does anybody have any idea what I'm doing wrong? Why can't I get the
code I need to compile out of this JAR file? And _why in the world_
can't my compiler see the<AbcCl> class? The "abc" directory is
_right there_, with an "AbcCl.class" and an "AbcCl.java" file right in
it?

With the exception of the manual typo in UseEm.java, the only thing
wrong with your procedure is that you should also have supplied '.' as
part of the classpath on your first javac attempt with the JARs
(otherwise javac doesn't know where UseEm.java is). But that absolutely
should have worked.

I know it should have because I just now did it. I always do just to
make sure there's nothing catching me out in a question. The only
difference is that I don't include *.java files in JARs.

Side note: 'jar' does not need a hyphen in front of its arguments, and
never has.

As another side note, do not mix classes that are in packages with classes that are not.

When one is ready to build jar files, then it may be time to drop
default package completely.

Arne
 
A

Arved Sandstrom

Arved said:
KevinSimonson wrote:
I'm having a nasty time figuring out how to use JAR files. I have a
directory "Jrs" that includes an "abc" directory and a "def"
directory. In directory "abc" I have a single file called
"AbcCl.java" coded:

package abc;
public class AbcCl
{
public String toString ()
{
return "[AbcCl Object]";
}
}

and in directory "def" I have a single file called "DefCl.java" coded:

package def;
public class DefCl
{
public String toString ()
{
return "[DefCl Object]";
}
}

Back in "Jrs" I have a file "UseEm.java" coded:

import abc.AbcCl;
import def.DefCl;

public class UseEm
{
public static void main ( Strin[] arguments)
{
AbcCl ac = new AbcCl();
DefCl dc = new DefCl();
System.out.println( "ac == " + ac + ", dc == " + dc + '.');
}
}

Then I executed "javac UseEm.java", which created three class files,
an "abc\AbcCl.class", a "def\DefCl.class", and a "UseEm.java".
Finally I created two jar files with commands "jar -cf abc.jar abc"
and "jar -cf def.jar def".

Then I created a "Jrs2" directory, a sibiling to "Jrs", and copied to
it "UseEm.java" and the two JAR files. In that "Jrs2" directory I
typed in the command "javac -cp abc.jar:def.jar", and as a result got
a whole bunch of error messages. I was kind of expecting that.

At that point I typed in "jar -xf abc.jar" which extracted the "abc"
directory, and I thought that therefore I should be able to compile
"UseEm.java" with the command "javac -cp .:def.jar UseEm.java". But
when I tried it I got error messages:
[ SNIP ]

6 errors

Does anybody have any idea what I'm doing wrong? Why can't I get the
code I need to compile out of this JAR file? And _why in the world_
can't my compiler see the<AbcCl> class? The "abc" directory is
_right there_, with an "AbcCl.class" and an "AbcCl.java" file right in
it?

With the exception of the manual typo in UseEm.java, the only thing
wrong with your procedure is that you should also have supplied '.' as
part of the classpath on your first javac attempt with the JARs
(otherwise javac doesn't know where UseEm.java is). But that absolutely
should have worked.

I know it should have because I just now did it. I always do just to
make sure there's nothing catching me out in a question. The only
difference is that I don't include *.java files in JARs.

Side note: 'jar' does not need a hyphen in front of its arguments, and
never has.

As another side note, do not mix classes that are in packages with
classes that are not.

When one is ready to build jar files, then it may be time to drop
default package completely.

Arne
That's good advice for the OP. I don't use the default package either.

In this case, though, with the imported classes being in named packages,
various quirks of the default package do not interfere with compiling
and running the OP's example. Hence it would be interesting to find out
what is causing the OP's problem.

AHS
--
You should know the problem before you try to solve it.
Example: When my son was three he cried about a problem with his hand. I
kissed it several times and asked him about the problem. He peed on his
hand.
-- Radia Perlman, inventor of spanning tree protocol
 
K

KevinSimonson

Arved Sandstrom wrote:
KevinSimonson wrote:
I'm having a nasty time figuring out how to use JAR files.  I havea
directory "Jrs" that includes an "abc" directory and a "def"
directory.  In directory "abc" I have a single file called
"AbcCl.java" coded:
package abc;
public class AbcCl
{
   public String toString ()
   {
     return "[AbcCl Object]";
   }
}
and in directory "def" I have a single file called "DefCl.java" coded:
package def;
public class DefCl
{
   public String toString ()
   {
     return "[DefCl Object]";
   }
}
Back in "Jrs" I have a file "UseEm.java" coded:
import abc.AbcCl;
import def.DefCl;
public class UseEm
{
   public static void main ( Strin[] arguments)
   {
     AbcCl ac = new AbcCl();
     DefCl dc = new DefCl();
     System.out.println( "ac == " + ac + ", dc == " + dc + '.');
   }
}
Then I executed "javac UseEm.java", which created three class files,
an "abc\AbcCl.class", a "def\DefCl.class", and a "UseEm.java".
Finally I created two jar files with commands "jar -cf abc.jar abc"
and "jar -cf def.jar def".
Then I created a "Jrs2" directory, a sibiling to "Jrs", and copied to
it "UseEm.java" and the two JAR files.  In that "Jrs2" directory I
typed in the command "javac -cp abc.jar:def.jar", and as a result got
a whole bunch of error messages.  I was kind of expecting that.
At that point I typed in "jar -xf abc.jar" which extracted the "abc"
directory, and I thought that therefore I should be able to compile
"UseEm.java" with the command "javac -cp .:def.jar UseEm.java".  But
when I tried it I got error messages:
[ SNIP ]
6 errors
Does anybody have any idea what I'm doing wrong?  Why can't I get the
code I need to compile out of this JAR file?  And _why in the world_
can't my compiler see the<AbcCl>  class?  The "abc" directory is
_right there_, with an "AbcCl.class" and an "AbcCl.java" file right in
it?
With the exception of the manual typo in UseEm.java, the only thing
wrong with your procedure is that you should also have supplied '.' as
part of the classpath on your first javac attempt with the JARs
(otherwise javac doesn't know where UseEm.java is). But that absolutely
should have worked.
I know it should have because I just now did it. I always do just to
make sure there's nothing catching me out in a question. The only
difference is that I don't include *.java files in JARs.
Side note: 'jar' does not need a hyphen in front of its arguments, and
never has.
As another side note, do not mix classes that are in packages with
classes that are not.
When one is ready to build jar files, then it may be time to drop
default package completely.

That's good advice for the OP. I don't use the default package either.

In this case, though, with the imported classes being in named packages,
various quirks of the default package do not interfere with compiling
and running the OP's example. Hence it would be interesting to find out
what is causing the OP's problem.

AHS
--
You should know the problem before you try to solve it.
Example: When my son was three he cried about a problem with his hand. I
kissed it several times and asked him about the problem. He peed on his
hand.
-- Radia Perlman, inventor of spanning tree protocol- Hide quoted text -

- Show quoted text -

Somebody on another forum helped me see the problem. I'm working on a
Windows 7 machine, so I should have been delimiting my classpath with
semicolons, not colons! I replaced the colons with semicolons and
everything worked just fine.

Kevin Simonson
 
A

Arne Vajhøj

Arved Sandstrom wrote:
KevinSimonson wrote:
I'm having a nasty time figuring out how to use JAR files. I have a
directory "Jrs" that includes an "abc" directory and a "def"
directory. In directory "abc" I have a single file called
"AbcCl.java" coded:

package abc;
public class AbcCl
{
public String toString ()
{
return "[AbcCl Object]";
}
}

and in directory "def" I have a single file called "DefCl.java" coded:

package def;
public class DefCl
{
public String toString ()
{
return "[DefCl Object]";
}
}

Back in "Jrs" I have a file "UseEm.java" coded:

import abc.AbcCl;
import def.DefCl;

public class UseEm
{
public static void main ( Strin[] arguments)
{
AbcCl ac = new AbcCl();
DefCl dc = new DefCl();
System.out.println( "ac == " + ac + ", dc == " + dc + '.');
}
}

Then I executed "javac UseEm.java", which created three class files,
an "abc\AbcCl.class", a "def\DefCl.class", and a "UseEm.java".
Finally I created two jar files with commands "jar -cf abc.jar abc"
and "jar -cf def.jar def".

Then I created a "Jrs2" directory, a sibiling to "Jrs", and copied to
it "UseEm.java" and the two JAR files. In that "Jrs2" directory I
typed in the command "javac -cp abc.jar:def.jar", and as a result got
a whole bunch of error messages. I was kind of expecting that.

At that point I typed in "jar -xf abc.jar" which extracted the "abc"
directory, and I thought that therefore I should be able to compile
"UseEm.java" with the command "javac -cp .:def.jar UseEm.java". But
when I tried it I got error messages:
[ SNIP ]

6 errors

Does anybody have any idea what I'm doing wrong? Why can't I get the
code I need to compile out of this JAR file? And _why in the world_
can't my compiler see the<AbcCl> class? The "abc" directory is
_right there_, with an "AbcCl.class" and an "AbcCl.java" file right in
it?

With the exception of the manual typo in UseEm.java, the only thing
wrong with your procedure is that you should also have supplied '.' as
part of the classpath on your first javac attempt with the JARs
(otherwise javac doesn't know where UseEm.java is). But that absolutely
should have worked.

I know it should have because I just now did it. I always do just to
make sure there's nothing catching me out in a question. The only
difference is that I don't include *.java files in JARs.

Side note: 'jar' does not need a hyphen in front of its arguments, and
never has.

As another side note, do not mix classes that are in packages with
classes that are not.

When one is ready to build jar files, then it may be time to drop
default package completely.
That's good advice for the OP.
:)

I don't use the default package either.

I would have fallen down my chair if you had said you did.
In this case, though, with the imported classes being in named packages,
various quirks of the default package do not interfere with compiling
and running the OP's example. Hence it would be interesting to find out
what is causing the OP's problem.

It was all side notes.

Arne
 
M

markspace

Somebody on another forum helped me see the problem. I'm working on a
Windows 7 machine, so I should have been delimiting my classpath with
semicolons, not colons! I replaced the colons with semicolons and
everything worked just fine.


*Smacks forehead*

The Solaris man page I linked to wasn't a clue? You couldn't say "I'm
running Windows not Solaris?"

OK, if you're new, it's easy to not understand there's a difference, but
jeeze folks, give us something to work with here. Anytime you're
running from the command line, please provide the *exact* output (and
input) so we can see it, including directory listings and command
prompts. You're just shorting yourself if you don't.

It's even easier to do that retyping everything, like you did. Just ...
wow. Please, make it easy on you and us, show us the whole thing, not
some carefully elided example that removes the actual information we need.
 
R

Roedy Green

I'm having a nasty time figuring out how to use JAR files. I have a
directory "Jrs" that includes an "abc" directory and a "def"
directory. In directory "abc" I have a single file called
"AbcCl.java" coded:

for a primer, see:

http://mindprod.com/jgloss/jar.html
http://mindprod.com/jgloss/jarexe.html
http://mindprod.com/jgloss/jarsignerexe.html

It is a lot easier to let ant/genjar build jars for you despite the
initial hurdle. After you get your first script working, everything
just works. You never have to think about it again.

see http://mindprod.com/jgloss/ant.html
http://mindprod.com/jgloss/genjar.html

--
Roedy Green Canadian Mind Products
http://mindprod.com
Capitalism has spurred the competition that makes CPUs faster and
faster each year, but the focus on money makes software manufacturers
do some peculiar things like deliberately leaving bugs and deficiencies
in the software so they can soak the customers for upgrades later.
Whether software is easy to use, or never loses data, when the company
has a near monopoly, is almost irrelevant to profits, and therefore
ignored. The manufacturer focuses on cheap gimicks like dancing paper
clips to dazzle naive first-time buyers. The needs of existing
experienced users are almost irrelevant. I see software rental as the
best remedy.
 
R

Roedy Green

Does anybody have any idea what I'm doing wrong?

See http://mindprod.com/jgloss/classpath.html

Many people have written saying this essay made it all clear. I found
it made no sense until I started speculating about how Javac.exe and
Java.exe go about finding class files. With a rough idea of the
algorithm in mind, suddenly all fell into place.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Capitalism has spurred the competition that makes CPUs faster and
faster each year, but the focus on money makes software manufacturers
do some peculiar things like deliberately leaving bugs and deficiencies
in the software so they can soak the customers for upgrades later.
Whether software is easy to use, or never loses data, when the company
has a near monopoly, is almost irrelevant to profits, and therefore
ignored. The manufacturer focuses on cheap gimicks like dancing paper
clips to dazzle naive first-time buyers. The needs of existing
experienced users are almost irrelevant. I see software rental as the
best remedy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top