Aod Ren
Aod Ren

Reputation: 679

How to know java package through a command line

Is there a way to know which java package a class is related to through a line of command? For exemple, I would like to print out the package of the class Integer which is java.lang.Integer

Thanks

Upvotes: 0

Views: 377

Answers (3)

Lumi
Lumi

Reputation: 15264

Quick & easy solution, simply abuse the classlist file. Not guaranteed to find all classes, of course, but should do the job for the standard case.

X:\Java ◄●++●► classlist.bat hashset
java/util/HashSet
java/util/LinkedHashSet
java/util/regex/IntHashSet

X:\Java ◄●++●► classlist.bat stringb
java/lang/AbstractStringBuilder
java/lang/StringBuffer
java/lang/StringBuilder

X:\Java ◄●++●► classlist.bat "reader writer"
…

X:\Java ◄●++●► type classlist.bat
@FINDSTR/I %* "C:\Program Files\Java\jdk-19\lib\classlist" | FINDSTR /VB "@ #"

Substitute the path to the classlist file with the one on your system. You can of course modify the second findstr to remove more lines according to your needs, for example everything to do with jdk/internal.

Update – In fact, the classlist file is utterly inadequate for the job as it holds only a small subset of the classes of general interest. You could build your own list file by filtering the parts of the JDK you're interested in from the (huge) output of the following command:

jimage list "C:\Program Files\Java\jdk-19\lib\modules"

Better method:

X:\Java ◄●++●► type jimrex.bat
@jimage list --include regex:.*%1.* "C:\Program Files\Java\jdk-19\lib\modules"
X:\Java ◄●++●► jimrex.bat java.io.*Reader
…

Upvotes: 0

theo
theo

Reputation: 995

If I understand correctly you want to be able to search for a class using its SimpleName and then print the package name. Or at least print all possible matches (found in several packages).

If this is the case you can leverage the snippet (found here) to create a utility yourself and execute from command line. This might be a bit slow though.

Upvotes: 0

Aaron
Aaron

Reputation: 24802

Edit : there's a class.getPackage() method I didn't knew about.

System.out.println(Integer.class.getPackage().getName());

Upvotes: 2

Related Questions