a java tool for the command line
jt
is a java tool for the command line. Only works with the Java8 stdlib.
You can either build it from source with go build
, or install it with
go install github.com/tsatke/jt@latest
After installing, the jt
command should work.
Everything in jt
is done with class paths, rather than qualified names (java/lang/Object
rather than java.lang.Object
).
In the future, this tool might support the qualified names as input, but for now, it just saves a lot of headaches if we just use the paths.
If you want more output (or think something might be wrong), you can check the debug output by adding the -v
flag.
Assuming you have a jar file with 5 classes in it (no matter where exactly, just inside the jar file), the following works.
$ jt classes 'myjar.jar'
com/mypackage/Class1
com/mypackage/Class2
com/mypackage/Class3
com/mypackage/Class4
com/mypackage/Class5
At the moment, jt
supports Maven and Eclipse project formats.
That is, for Maven, it uses the pom.xml
and for Eclipse the .classpath
file to get a classpath.
On that classpath, jt
will search for classes.
Currently, it will always consider the JAVA_HOME
variable and use that as the standard library on any classpath, regardless what the Maven or Eclipse project have configured.
If JAVA_HOME
is not set, you will not be able to get information about classes that are located in the standard library.
jt
can display the classpath of a project.
The standard library is always on top of other entries on the classpath.
$ jt classpath
/path/to/my/project/src/main/java
/path/to/java-home/lib/rt.jar
...
/path/to/java-home/lib/charsets.jar
/path/to/maven-repo/junit/junit/4.11/junit-4.11.jar
/path/to/maven-repo/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
You can use jt
to find classes on the classpath of the project that you are currently in.
$ jt find 'App'
Project results:
com/mypackage/App
com/mypackage/App$Builder
Classpath results:
com/thirdparty/AppConfiguration
but
$ jt find 'App' | cat
com/mypackage/App
com/mypackage/App$Builder
com/thirdparty/AppConfiguration
if you're not on a terminal, jt
will not print the headers, so you can use grep
, xargs
and your other favorite tools as you're used to.
In addition, if you know that a specific class is in your project, and you don't need to search a (potentially) large classpath, you can pass the --no-classpath
option.
This will keep jt
from even building a classpath, and save you a lot of time, especially in Maven projects.
You can view the superclasses of a given class on the classpath. Interfaces are not implemented yet.
$ jt superclass 'com/mypackage/io/SpecialInputStream'
com/mypackage/io/SpecialInputStream
com/mypackage/io/AbstractInputStream
java/io/InputStream
java/lang/Object
You can view the direct subclasses of a given class on the classpath. The results can be filtered by a regex pattern, which will speed up searches by a lot depending on the regex pattern.
$ jt subclass 'java/lang/Object'
... (all classes from the stdlib that extend java.lang.Object)
com/mypackage/App
com/mypackage/App$Builder
With a filter, the search effectively skips all the standard library classes, and the search will complete a lot faster.
$ jt subclass 'java/lang/Object' 'com/mypackage/.*'
com/mypackage/App
com/mypackage/App$Builder
With the --invert
option, you can also invert the pattern, meaning that all classes that DO NOT match the pattern are checked.
The search is performed on multiple goroutines concurrently.
The number of goroutines used is equal to runtime.NumCPU()
.