1

I am studying (cramming) for the AP Computer Science test. One of the requirements is the need to familiarize oneself with a program called "GridWorld." In my studies, I have been running and compiling my code using the terminal, instead of an IDE. In one directory, there is a program called BugRunner.java that must be compiled. The classes are in a .jar file called gridworld.jar. Gridworld.jar is in a separate directory. How do I configure the javac compiler to use the classes? I use a Mac with a bash shell. I tried typing

javac -cp .:/Users/raidenworley/programming/java/apcomp/GridWorldCode/projects/firstProject/gridworld.jar BugRunner.java

into the terminal, but I then receive the following:

BugRunner.java:17: package info.gridworld.actor does not exist
import info.gridworld.actor.ActorWorld;
                           ^
BugRunner.java:18: package info.gridworld.actor does not exist
import info.gridworld.actor.Bug;
                           ^
BugRunner.java:19: package info.gridworld.actor does not exist
import info.gridworld.actor.Rock;
                           ^
BugRunner.java:33: cannot find symbol
symbol  : class ActorWorld
location: class BugRunner
        ActorWorld world = new ActorWorld();
        ^
BugRunner.java:33: cannot find symbol
symbol  : class ActorWorld
location: class BugRunner
        ActorWorld world = new ActorWorld();
                               ^
BugRunner.java:34: cannot find symbol
symbol  : class Bug
location: class BugRunner
        world.add(new Bug());
                      ^
BugRunner.java:35: cannot find symbol
symbol  : class Rock
location: class BugRunner
        world.add(new Rock());
                      ^
7 errors
Raiden Worley
  • 394
  • 1
  • 4
  • 14
  • If this was eclipse, simply right-clicking the project and getting configure-path tab in then choosing "add external jar". Otherwise http://stackoverflow.com/questions/2079635/how-can-i-set-the-path-variable-for-javac-so-i-can-manually-compile-my-java-wor gives some results. – huseyin tugrul buyukisik May 22 '13 at 16:53
  • That's all in windows, which I don't have. – Raiden Worley May 22 '13 at 17:01

2 Answers2

2

Are you referring to the jar within your -cp since I didn't see it.

You can do it by exporting out your class path.

Take a look here

adding classpath in linux

As you have mentioned you was referring to the path containing java file rather than jar.

You should be able to refer your folder containing the jar without calling physical jar. I only said this to ensure it was available. Anyhow if you wish for advanced methods of exporting class paths. Useful fir auotmated cron calls etc have a read of this Can I export a variable to the environment from a bash script without sourcing it?

Community
  • 1
  • 1
V H
  • 8,382
  • 2
  • 28
  • 48
  • The jar is referred to. The classpath is /Users/raidenworley/programming/java/apcomp/GridWorldCode/projects/firstProject/gridworld.jar – Raiden Worley May 22 '13 at 17:17
0

I managed to solve it. I typed into the terminal:

 javac -cp .:../../gridworld.jar BugRunner.java

And then ran it using

java -cp .:../../gridworld.jar BugRunner

I don't understand what was wrong, or why this worked.

Raiden Worley
  • 394
  • 1
  • 4
  • 14