How to find the jar file (with maven artifacts) for a class
Go to the site https://jar-download.com/, select ‘Class search’ and enter your class name.
Method References
There are four kinds of method references:
Kind | Example |
---|---|
Reference to a static method | ContainingClass::staticMethodName |
Reference to an instance method of a particular object | containingObject::instanceMethodName |
Reference to an instance method of an arbitrary object of a particular type | ContainingType::methodName |
Reference to a constructor | ClassName::new |
How to encode a URL string?
In a URL not all characters are allowed. Therefore strings have to be URL encoded.
This is done by the java method: java.net.URLEncoder.Encode(<string>, <character-encoding>)
.
Where the <character-encoding> is typically set to “UTF-8”.
How to parse command line arguments?
Use the package ‘org.apache.commons.cli
‘.
How to store temporary file (or how to use jimfs)
To store temporary files use com.google.common.jimfs.Jimfs.
However, to iterate over the contents a DirectoryStream (obtained via Files.newDirectoryStream(path)) doesn’t seem to work.
Instead use Files.walkFileTree(path, simpleFileVisitor<Path>).
How to create a Windows executable for a Java application
One option for doing this is jlink. However jlink only works if all jar files used are based on modules. As almost everyone uses open source third party libraries, of which many aren’t based on modules, this is actually no option.
How to structure a JavaFx application.
If anything goes wrong in your application, you want to log it. So start with setting up logging (see #howToUseLogging).
I call logSetup()
in the constructor of the JavaFx application.
Check the java runtime version.
This is not needed if you create a windows installation .exe for your application, which is bundled with the correct JRE.
But otherwise it makes sense to check it here, which can be done with the following code:
Runtime.Version version = Runtime.version();
if (version.feature() < MIN_JAVA_FEATURE_NUMBER) { // where MIN_JAVA_FEATURE_NUMBER is an int
DefaultCustomizationFx.getInstance().getComponentFactoryFx()
.createErrorDialog("Java runtime version too old",
"You need at least version " + MIN_JAVA_FEATURE_NUMBER + ", but you have " +version.feature())
.showAndWait();
Platform.exit();
}
How to use logging
For logging I use java.util.logging
. There are several logging libraries, but for me java.util.logging
does what I need and it doesn’t introduce extra dependencies.
Logging is initialized by calling logSetup()
in goedegep.jfx.JFXApplication
(which is extended by each application). This installs a goedegep.util.logging.MyLoggingFormatter
.
When, during development, you run your applicaton in Eclipse, you use logging on the console. So you there’s no need to log to a file.
For an installed application you don’t have a console, so you have to log to a file.
When you install your application under Windows, it will be under C:/Program Files
. When you run your application, the user directory will be this installation directory. As this is not writable in user mode, this isn’t a good location for the log file.
So either the location of the log file has to be specified by the user, or you can log to a subdirectory of the users home directory.
Every class defines its own logger of type java.util.logging.Logger
:
private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName());