Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

lang



The lang package contains additional classes which work with the Java lang package.

SystemUtils

The SystemUtils class provides several utilities adding functionalities to the System class:
  • Returning true if the current JVM version is at least equal to a specified version
  • Returning the major version of the current JVM version
  • Returning the platform type (Windows, Unix, Linux, Mac OS X)
  • Returning if the platform is 64 bits or 32 bits
  • Returning true if an executable file is compatible with the OS architecture used by the JVM. The formats which are supported are:
    • Portable Executable Format executable on Windows for 32 bits or 64 bits
    • Executable and Linkable Format (elf) executable on Unix-like systems for 32 bits or 64 bits
    • Mach-O executable on Mac OS X for 32 bits or 64 bits (Universal binaries are not supported)

StringUtils

The StringUtils class contains a one utility method allowing to convert non ASCII characters to their associated XML escaped representation.

HTMLColors

The HTMLColors class allows to decode an html color and convert it to its hexadecimal representation. For example:
  String color = HTMLColors.decodeColor("00CC00"); // return "#00CC00"
  color = HTMLColors.decodeColor("#00CC00"); // return "#00CC00"
  color = HTMLColors.decodeColor("black"); // return "#000000"
  color = HTMLColors.decodeColor("white"); // return "#FFFFFF"
  color = HTMLColors.decodeColor("White"); // return "#FFFFFF"
  color = HTMLColors.decodeColor("cornflowerblue"); // return "#6495ED"
  color = HTMLColors.decodeColor("cornflowerBlue"); // return "#6495ED"

HTMLSwingColors

The HTMLSwingColors class allows to decode an html color and convert it to its associated AWT color. For example:
  Color color = HTMLSwingColors.decodeColor("00CC00"); // return Color(0, 204, 0)
  color = HTMLSwingColors.decodeColor("#00CC00"); // return Color(0, 204, 0)
  color = HTMLSwingColors.decodeColor("black"); // return Color(0, 0, 0)
  color = HTMLSwingColors.decodeColor("white"); // return Color(255, 255, 255)
  color = HTMLSwingColors.decodeColor("White"); // return Color(255, 255, 255)
  color = HTMLSwingColors.decodeColor("cornflowerblue"); // return Color(100, 149, 237)
  color = HTMLSwingColors.decodeColor("cornflowerBlue"); // return Color(100, 149, 237)

HTMLEscaper

Main Article: HTMLEscaper

The HTMLEscaper class allows to:
  • convert a text with HTML entities to be usable in XML. HTML entities are converted to the associated XML content
  • convert text characters to their corresponding HTML entities


For example:
  • The é or è text will be converted to The é or è text
Note that this class can be used safely in a multithreaded environment.

ResourceLoader

Main Article: ResourceLoader

The ResourceLoader class allows to retrieve resources in an application or library Jar files.

By default the resources will be retrieved using the context Class Loader of the current Thread.

Example

For example, suppose that we have the following class structure:
      ==> my
      ====> resources
      ======> resource.xml
      ====> package
      ======> MyClass.java
We can use the following code to retrieve the resource.xml resource in the MyClass class:
   public class MyClass {
     public static void main(String[] args) {
       ResourceLoader loader = new ResourceLoader("my/resources");
       URL resourceURL = loader.getURL("resource.xml");
     }
   }

   ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();
   URLClassLoader loader = new URLClassLoader(urls, parentLoader);

   ResourceLoader loader = new ResourceLoader("my/package", loader);
   URL resourceURL = loader.getURL("resource.xml");

MessageConstructor

Main Article: MessageProvider

The MessageConstructor class allows to construct a message from a template and several variables.

MessageProvider

Main Article: MessageProvider

The MessageProvider class provides bundles of messages. It manages several bundles of messages, which are simply property files. These property files associate keys with String messages.

The way to use this class is:
  • Create one or several bundles. For example:
       MessageProvider provider = MessageProvider.getInstance();
       provider.createBundle("aKey", "org/myresources/", "messages.properties");
    
    or
       MessageProvider provider = MessageProvider.getInstance();
       provider.createBundle("anotherKey", url);
    
  • Reference one bundle and get some messages anywhere in the code:
       MessageBundle bundle = MessageProvider.getBundle("aKey");
       String message = bundle.getMessage("propertyName");
    

JarDependenciesFinder

The JarDependenciesFinder class allows to get the array or list of Class-Path dependencies of one jar file or a list of jar files. The dependencies are retrieved from the jar file Manifest Class-Path property.

Note that:
  • The class tries not to include twice the same jar file in the result
  • The resulting URLs are all absolute. Relative dependencies will be correctly handled
For example, for the following jar file Manifest, for the following Jar file: L:/my/JarFile.jar:
   Class-Path: myJar1.jar \
   myJar2.jar
The JarDependenciesFinder.getJarDependencies(URL, boolean) will return the following URLs with include set to false:
  • L:/my/myJar1.jar
  • L:/my/myJar2.jar

ExecutableFinder

The ExecutableFinder class allows to find an executable among several roots. The class will return a list of executables valid with the Platform architecture.

Example

Suppose that we look for the python executable anywhere on a top-level directory.
   ExecutableFinder finder = new ExecutableFinder(false);
   finder.addAllRoots();
   List<File> files = finder.getExecutables("python*", "*.exe");

Suppose that we want to get the Netbeans executables on Windows. We can do:
   ExecutableFinder finder = new ExecutableFinder(false);
   finder.addRootFromEnv("programfiles");
   List<File> files = finder.getExecutables("netbeans*", "bin/*.exe");

SerializingCloner

The SerializingCloner class allows to deep copy objects by serializing and unserializing them in memory. Note that classes must be serializable for the cloner to work.

Example

Suppose that we want to deep-copy a PlainDocument. It is not possible to clone instances of this class because the PlainDocument class does not implement the Cloneable interface, but is is serialisable, so we can perform:
   PlainDocument myDoc = ...
   SerializingCloner cloner = new SerializingCloner<PlainDocument>();
   PlainDocument doc = cloner.deepCopy(myDoc);

JREVersionDetector

Main Article: JREVersionDetector

The JREVersionDetector class allows to get the version of a JRE at a specified location, or compare this version with one or two specified version numbers. This class works by trying to start a JVM as an external process with the following command: java -version.

It is also possible to:
  • Specify a timeOut for launching the JVM as an external process (the default timeOut is 200 ms)
  • Specify a constraint on the JVM architecture (32 bits or 64 bits)

ProcessManager

The ProcessManager class allows to manage external processes. It allows to:
  • Get the list of external processes currently running, eventually sorted by the process name or upstart time
  • Kill a running process, with eventually its children processes recursively. Note that a timeOut can be used to block until the process have been effectively killed


It is possible to sort the processes by their command or application name. For example the following code will get on Windows the python processes sorted by their creation date:
   ProcessManager manager = new ProcessManager();
   SortedMap<Long, List<ExternalProcess>> processes = manager.getProcessByCreationDate("python.exe");
Each process found by the ProcessManager have the following methods:
  • Get the pid of the process
  • Get the command or application name
  • Get the parent process
  • Get the children processes
  • Get the upstart time. The upstart time is a long, and represents the EPOCH time. It is consistant with the result of the System.currentTimeMillis() method

Platform dependency

The class uses implementation classes for each Platform (WIndows, LInux, Mac OS X, UNIX), but appart from that, the code is pure Java and not platform-dependant. Note that:

ProcessManager kill algorithm

Note that if the ProcessManager.isFilteringSystem() is true, system the class will refuse to kill system processes. These systems are:
  • "root" processes on Linux and other Unixes platforms
  • "Services" processes on Windows


The algorithm which will be used by the manager to kill the process is the following:
  • If getKillProcessTimeOut() is less than 0 (the default), then no wait will be performed after the execution of the KILL command
  • else:
    • For i = 1 to getKillProcessTimeOutCount():
      • Wait until getKillProcessTimeOut()
      • If the process is not running anymore, then exit

NestedURLClassLoader

Main Article: NestedURLClassLoader

The NestedURLClassLoader class allows to load classes which are in jar files nested inside archives.

NativeLibraryLoader

The NativeLibraryLoader class allows to set the java.library.path property after the start of the JVM. Note that this method will only work for Java 8 up to Java 14.

The java.library.path is read only once when the JVM starts up. If you change this property using System.setProperty, it won't make any difference.

The way this class work is by setting it to null prior to setting the new path. This method will unload the previously specified paths and replace them by the new ones.

Note that this method will use a different mechanism for Java 8 and Java 9, and for Java 10 up to Java 14.

MacOSXUISetter

Main Article: MacOSXUISetter

The MacOSXUISetter class allows to:
  • Set the menus on the top of the screen as it is customary for Mac OS X applications
The SwingMacOSXUISetter class allows to:
  • Set the menus on the top of the screen
  • Set the application name
  • Set the Mac OS X Look and Feel
Note that these classes won't perform anything if the platform is not Mac OS X.
Note that you must call these classes before you are using any Swing classes, including Swing constructors.

AppLauncher

Main Article: AppLauncher

The AppLauncher class launch a process to start a Java jar file.

This class allows to:
  • Specify the module paths (the --add-modules declaration)
  • Specify the --add-opens declarations
  • Specify the --add-exports declarations
  • Specify the required Java version
  • Specify the allowed custom properties
  • Specify the path of the main Class

This class and all its dependencies is on the AppLauncher.jar package. It has no dependencies on other packages.

Categories: lang | packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences