Your instinct is right, and it is worth stating plainly: there is no single command that gives a trustworthy yes or no. Anyone who tells you otherwise is describing a first pass, not an answer.
The reason is packaging. A Java library can be present as:
- A file installed by your distribution's package manager, which is the easy case.
- A jar shipped inside a vendor's application directory, invisible to the package manager.
- A jar nested inside another archive — a war or ear or fat jar — so a filename search finds nothing.
- A class shaded into a completely differently-named artefact, where even the nested filename is gone.
Each of those needs a different detection method, and the last one defeats file-based searching entirely.
So the practical approach is layered, and you should treat each layer as reducing uncertainty rather than resolving it.