1

I need to check if a specific class is available from current directory using a default JRE/JDK command line tool.

I could build my own class to list it or check if a specific class is reacheble from current directory and current CLASSPATH environment variable, but this option is not available since I need to check if a specific class name is available or not in a protected production environment (read only).

Rafael Borja
  • 4,487
  • 7
  • 29
  • 33

2 Answers2

5

You can use javap:

$ javap java.lang.String
Compiled from "String.java"
public final class java.lang.String implements java.io.Serializable
[...]


$ javap no.such.Class
Error:  class not found: no.such.Class
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • possible to do this, in case you're trying to find a class on specific jar files. `javap -cp "file1.jar:file2.jar" com.example.ClassYouLookFor` – nanangarsyad May 08 '21 at 23:46
3

You can use the -verbose option of the java command and search for the fully qualified name of a specific class.

$ java -verbose -jar MyProgram.jar | grep "java.lang.String"
[Loaded java.lang.String from /Library/Java/…/Contents/Home/jre/lib/rt.jar]
[Loaded java.lang.StringBuffer from /Library/Java/…/Contents/Home/jre/lib/rt.jar]
…

Addendum: I want to check the class availability for an environment.

If you are running from the java command line, either the paths specified in the -classpath option or the CLASSPATH environment variable will be searched. If you are running from a JAR, the manifest's Class-Path attribute, for example, supplants these settings.

If you are trying to find a required JAR that may not be accessible in these ways, you'll have to search the file system. I use a combination of find, jar and grep, typically focussing on paths defined in system properties such as java.endorsed.dirs and java.ext.dirs; several related approaches are shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045