0

I am currently writing a program that uses an optional library at runtime. Removing and adding this library during testing runs just fine (compiles, runs, no errors), because the associated classes are never called unless the library is present.

However, when said library is removed, of course Eclipse throws a bunch of "cannot be resolved" errors on those classes - even though they will never be loaded.

Is there any way to suppress these errors on these specific classes (as I know that they are - in effect - false positives), or is there a more elegant way to solve this?

Weckar E.
  • 727
  • 2
  • 5
  • 19
  • 1
    Compile with the sources and package (jar) your project without them – Lino Mar 12 '19 at 15:10
  • 1
    @Lino Please clarify? That would still throw up errors during testing, would it not? – Weckar E. Mar 12 '19 at 15:11
  • 3
    If you have source code depending on a library, that library must be present at build time. What else are you expecting? – Seelenvirtuose Mar 12 '19 at 15:11
  • @Seelenvirtuose It runs just fine without that code, because the classes that use that code are never called. So, your statement is in this case false. – Weckar E. Mar 12 '19 at 15:12
  • 1
    @WeckarE. If you use a maven as your build tool you can mark these dependencies as `provided` – Lino Mar 12 '19 at 15:12
  • 2
    @Lino using gradle, actually, though I seem to remember it has a similar function... Good call. – Weckar E. Mar 12 '19 at 15:13
  • 2
    @WeckarE. You speak about Eclipse and a bunch of compile time errors in your question. This cannot be overgone. In your comment, you now speak about runtime. These are two very different times. Your statement is false! – Seelenvirtuose Mar 12 '19 at 15:14
  • Your compiller just say you that there is a missing library that can occured in an Exception while running your app, but anyway you can still launch your app. – 0ddlyoko Mar 12 '19 at 15:31

1 Answers1

1

Assuming we arent using any special frameworks whatsoever:

So let's assume you got a program, you used a library. Your code looks something like this now within a certain class:

LibraryClass c;

and within a method you got:

c.runSomeMethod();

you run the code.

then you remove the line c.runSomeMethod();

you would think that the LibraryClass is not used anymore and you wouldnt need the Library.

But in actuality your compiler runs through the line

LibraryClass c;

and tries to resolve c to the type "LibraryClass" in order for c to be properly assigned.

for that you NEED the library. Leaving the Library out of your project results in c not beeing able to be resolved to LibraryClass in this examplatory case

Alan
  • 949
  • 8
  • 26