Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

JarDependencies



There are two ways to create a JarDependencies:
  • Either create a JarDependencies and provide the path of the jar file
  • Or provide the directory to look for, and the root name of the jar file (excluding the extension). This method will allow to get a file having the required fiklename pattern in the provided directory

Using the path of the jar file

The simplest way to create a JarDependencies is simply by using the JarDependencies(File) method, and provide a directory containing classes, or one jar file:
  File myJarFile = "D:/Java/libraries/theJarFile.jar";
  JarDependencies depend = new JarDependencies(myJarFile);

Using the directory

It is also possible to create a JarDependencies by specifying a directory and using a file name pattern, by using one of the following methods: These methods will look for any files with the root name in the directory, having characters specifying the version[1]
Such as myFile-1.2.4.jar for example
. The arguments used for the method are:
  • The directory which will contain the file to look for
  • The root name of the file
  • A boolean specifying if a file which which only have the root name, without any additional version characters will be accepted[2]
    Such as myFile.jar for example
  • An optional short value[3] specifying if the character to use to separate the elements of the filename pattern will be a dash or an undescore[4]
    By default a dash character will be used as the separator

If no file with the specifies name pattern can be found in the directory, or more than one file, then the constructor will throw an IOException.


This option simplifies the usage of the library when using Maven, or more generally when the jar file produced by the build system contains the version name.

Root name pattern

The root name must have the following pattern: ([a-zA-Z]|[a-zA-Z_][a-zA-Z_\-0-9]*)[a-zA-Z_0-9]

Use with a default dash

For example:
   File myDirectory = "D:/Java/libraries";
   JarDependencies depend = new JarDependencies(myDirectory, "theJarFile", false);
This method has the following arguments
  • The first argument is the directory where to find the file
  • The second argument if the root name iof the file (excluding the .jar or .zip extension
  • The last argument specifies if files with this exact name are allowed
The constructor will look for files with the .jar or .zip extension which are children files in the directory:
  • Either with the exact file name (without the extension) if the last argument is true
  • Or with the following pattern: >rootname>-[0-9a-zA-Z]*([\-0-9a-zA-Z\.]*)?(\.jar|\.zip)

Use with an underscore

For example:
   File myDirectory = "D:/Java/libraries";
   JarDependencies depend = new JarDependencies(myDirectory, "theJarFile", false, NamePatternSeparator.UNDERSCORE);
This method has the following arguments
  • The first argument is the directory where to find the file
  • The second argument if the root name iof the file (excluding the .jar or .zip extension
  • The third argument specifies if files with this exact name are allowed
  • The last argument specifies that wee look for an underscore charactert to separate the elements of the filename pattern
The constructor will look for files with the .jar or .zip extension which are children files in the directory:
  • Either with the exact file name (without the extension) if the last argument is true
  • Or with the following pattern: >rootname>_[0-9a-zA-Z]*([_0-9a-zA-Z\.]*)?(\.jar|\.zip)

Example with a file

Suppose the following lib directory:
      -- library
      ---- test.jar
      ---- test2-2.3.4.jar
  • Calling JarDependency depend = new JarDependencies(new File(<lib directory>, "test.jar")) will return the JarDependencies for the library/test.jar file
  • Calling JarDependencies depend = new JarDependencies(new File(<lib directory>,"test2-2.3.4.jar")) will return the JarDependencies for the library/test2-2.3.4.jar file
  • Calling JarDependencies depend = new JarDependencies(new File(<lib directory>,"test2.jar")) will throw an IOException

Example with a directory

Example with a dash

Suppose the following lib directory:
      -- library
      ---- test.jar
      ---- test2-2.3.4.jar
      ---- test3-2.3.4b1.jar
      ---- test4-2.4.6-Snapshot.jar
  • Calling JarDependencies depend = new JarDependencies(<lib directory>, "test", true) will return the JarDependencies for the library/test.jar file
  • Calling JarDependencies depend = new JarDependencies(<lib directory>, "test2", false) will return the JarDependencies for the library/test2-2.3.4.jar file
  • Calling JarDependencies depend = new JarDependencies(<lib directory>, "test3", false) will return the JarDependencies for the library/test3-2.3.4b1.jar file
  • Calling JarDependencies depend = new JarDependencies(<lib directory>, "test4", false) will return the JarDependencies for the library/test4-2.4.6-Snapshot.jar file
  • Calling JarDependencies depend = new JarDependencies(<lib directory>, "test5", false) will throw an IOException

Example with an underscore

Suppose the following lib directory:
      -- library
      ---- test.jar
      ---- test2_2.3.4.jar
      ---- test3_2.3.4b1.jar
      ---- test4_2.4.6_Snapshot.jar
  • Calling JarDependencies depend = new JarDependencies(<lib directory>, "test", true, NamePatternSeparator.UNDERSCORE) will return the JarDependencies for the library/test.jar file
  • Calling JarDependencies depend = new JarDependencies(<lib directory>, "test2", false, NamePatternSeparator.UNDERSCORE) will return the JarDependencies for the library/test2_2.3.4.jar file
  • Calling JarDependencies depend = new JarDependencies(<lib directory>, "test3", false, NamePatternSeparator.UNDERSCORE) will return the JarDependencies for the library/test3_2.3.4b1.jar file
  • Calling JarDependencies depend = new JarDependencies(<lib directory>, "test4", false, NamePatternSeparator.UNDERSCORE) will return the JarDependencies for the library/test4_2.4.6_Snapshot.jar file

Notes

  1. ^ Such as myFile-1.2.4.jar for example
  2. ^ Such as myFile.jar for example
  3. ^ See NamePatternSeparator
  4. ^ By default a dash character will be used as the separator

Categories: general

Copyright 2019 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 licence