Given a java package x.y.z, can I alias x.y.z to a shorter name, so that i can then refer to java classes inside the package as my-alias.MyJavaClass.
If that isn't possible, I could just import
all classes into my namespace, but I don't want to specify the names of each class manually, and the clojure API docs doesn't seem clear on whether it's possible to import
all classes in a package automatically.
Package names are written in all lower case to avoid conflict with the names of classes or interfaces. Companies use their reversed Internet domain name to begin their package names—for example, com. example. mypackage for a package named mypackage created by a programmer at example.com .
Java compiler imports java. lang package internally by default.
A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories: Built-in Packages (packages from the Java API)
To import java package into a class, we need to use java import keyword which is used to access package and its classes into the java program. Use import to access built-in and user-defined packages into your java source file so that your class can refer to a class that is in another package by directly using its name.
There isn't any functionality for this (yet). Java packages are in a different namespace than Clojure's, so the usual alias tricks won't work.
What you can do is import each class, which lets you avoid the full package+class name:
(import [java.io File Writer Reader])
(new File "/")
; #<File />
Rich provides his reasons for not supporting (import [java.io.*])
here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With