I have the following class that uses JAX-RS to consume an api:
public class PackageProcessor implements IPackageProcessor {
public PackageProcessor() {
}
@Override
public PackageDimensions getPackageDimensions(byte[] image) throws PackageProcessorException {
Image imageBase64 = toBase64(image);
PackageDimensions dimensions = null;
try {
Client client = ClientBuilder.newClient();
dimensions = client.target(BASE_URI)
.path(DIMENSIONS_PATH)
.request(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.post(Entity.json(imageBase64))
.readEntity(PackageDimensions.class);
} catch (WebApplicationException ex) {
throw new PackageProcessorException("Error al procesar la imagen", ex);
}
return dimensions;
}
}
I'm trying to use it in a Java Application just to test if it works:
public static void main(String[] args) {
try {
IPackageProcessor p = new PackageProcessor();
Path path = Paths.get("path");
byte[] data = Files.readAllBytes(path);
PackageDimensions d = p.getPackageDimensions(data);
} catch (IOException ex) {
Logger.getLogger(Prueba.class.getName()).log(Level.SEVERE, null, ex);
} catch (PackageProcessorException ex) {
Logger.getLogger(Prueba.class.getName()).log(Level.SEVERE, null, ex);
}
}
But when I try to run this application I get the following error in the line doing the new PackageProcessor()
:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/WebApplicationException
at prueba.Prueba.main(Prueba.java:25)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.WebApplicationException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I understand what it means but I'm not calling anything that throws that in the constructor, am I?
I don't want to have to add a dependecy to JAX-RS in my java application, and that's way I'm encapsulating a possible WebApplicationException
inside my own exception class PackageProcessorException
. I thought that would be enough.
What am I doing wrong?
I'm using Netbeans, if that changes anything
Thanks for the explanations which seem correct, but if that's the case there is no way my java application can ignore the implementation of the PackageProcessor
?
That was what I was hoping for.
Your class PackageProcessor
uses WebApplicationException
class, so this class has to be initialized when you create an instance of your class.
The same in more details:
https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4.1 : your class is initialized when its instance is created.
T is a class and an instance of T is created
When it gets initialized, it tries to load all the classes it references, including WebApplicationException
(which is referenced by your code). As this class cannot be found, an error occurs.
Wrapping WebApplicationException
instance in your exception class is not enough, as first you have to catch WebApplicationException
, and to do that, you need to have the class (i.e. load it before).