Steve
Steve

Reputation: 4566

Compiling with Ant

I have two .class files that I'm supposed to black box test. These are in a package one.two.three. My tests are also in the same package. There is a third .class file in the same package whose purpose is to hold an enum variable for the Orders class I'm supposed to test. In eclipse, I'm able to get the junit tests for Orders to work by importing the enum directly e.g.

import one.two.three.Orders.ShippingMethod;

If I try to do this using Ant or via the command line, I get the error "package one.two.three.Orders does not exist". If I change the import statement to

import one.two.three.*;

Ant, Eclipse, and the terminal cannot find any of the classes I have. I need to compile and run the test cases with Ant. The classes are in bin/one/two/three Any help would be greatly appreciated, thanks.

Upvotes: 0

Views: 95

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70909

Import Orders, as it is the class, and assuming that ShippingMethod is an enum within that class, the correct way to reference its type is Orders.ShippingMethod.

Attempting to import a class's internal types sometimes works oddly in Eclipse. This is likely due to Eclipse not using the javac compiler packaged in your jdk, while Ant does (it has to, because Ant doesn't ship an embedded compiler).

 import one.two.three.Orders;


 public class Whatever {

    private Orders.ShippingMethod shipMethod;

 }

This should work in everything, as it's the right way to do it.

 import one.two.three.Orders.ShippingMethod;

could easily confuse most compilers as there is not a

 one/two/three/Orders/ShippingMethod.class

file, which means the class loader won't find it at runtime.

I'll bet it's a bug in the Eclipse embedded compiler, as I've seen quite a few. On the bright side, the Eclipse embedded compiler exists to provide faster, tighter integration between code editing and Eclipse. On the dark side, that means that sometimes the Eclipse compiler and the javac compiler differ. When in doubt, the javac compiler is probably correct.

Upvotes: 1

Jean Waghetti
Jean Waghetti

Reputation: 4727

You'll need to set the classpath.

I don't know exactly on Eclipse (I use NetBeans), but I click on Libraries -> add JAR/Folder.

For command line, you need to specify class path

java -cp path/to/my/files (...)

Upvotes: 0

Related Questions