Class JarTool

java.lang.Object
org.opensourcephysics.tools.JarTool
All Implemented Interfaces:
Runnable, Tool

public class JarTool extends Object implements Tool, Runnable
This provides a simple way to package files in a single JAR or ZIP file
Version:
1.0
Author:
Francisco Esquembre (http://fem.um.es), Doug Brown
  • Method Details

    • getInstance

      public static JarTool getInstance()
      for LaunchBuilder Gets the shared JarTool.
      Returns:
      the shared JarTool
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • send

      public void send(Job job, Tool replyTo)
      Sends a job to this tool and specifies a tool to reply to.
      Specified by:
      send in interface Tool
      Parameters:
      job - the Job
      replyTo - the tool to notify when the job is complete (may be null)
    • disableWarning

      public static void disableWarning()
      This method is kept for backwards compatibility. It is equivalent to alwaysOverwrite().
    • neverOverwrite

      public static void neverOverwrite()
      Sets the overwrite feature to false. By default, the packaging methods create() and append() will warn the user before overwritting a duplicated file. Calling this method before running those methods disables this warning and makes sure a new file will NEVER overwrite an older one. The methods create() and append() set the warning feature back.
      See Also:
      • alwayOverwrite
    • alwaysOverwrite

      public static void alwaysOverwrite()
      Sets the overwrite feature to true. By default, the packaging methods create() and append() will warn the user before overwritting a duplicated file. Calling this method before running those methods disables this warning AND makes sure a new file will ALWAYS overwrite an older one. The methods create() and append() set the warning feature back.
      See Also:
    • setOwnerFrame

      public static void setOwnerFrame(Frame owner)
      called by LaunchBuilder Sets the owner frame for progress dialogs that may appear when creating a JAR file.
      Parameters:
      owner - Frame
    • create

      public File create(ArrayList<String> sources, File parent, File target, Manifest manifest)
      called by LaunchBuilder Creates a JAR file out of the list of contents provided. Each entry in the sources list can be either a single file, a directory, or a compressed (ZIP, JAR or TRZ) file. Regular files and directories must exist under the given parent directory, and are saved with the same relative path as provided. As an exception, you can also add files in parent directories, such as, "../../../WhateverDir/WhateverFile.xml", but ALL leading "../" will be removed when saved. Directories are added recursively. Compressed files can, on the contrary, be placed anywhere and their contents are stored with the same directory structure as in the original jar file, irrespective of the original location of the source compressed file. If a file appears more than once, succesive files could overwrite previous ones. The user will be warned of this, except if either neverOverwrite() or alwaysOverwrite() have been invoked immediately before calling this method. (Calling this method resets the warning feature back.)
      Parameters:
      sources - ArrayList The list of content files to add. Each item in the list is a String with the relative name of a file or directory under the given parent directory, or of a compressed file anywhere in the hard disk.
      parent - File The parent directory for all relative filenames
      target - File The target compressed file. Its name must end in .zip, .jar or .trz. The user will be prompted to confirm the target.
      manifest - Manifest A manifest for the newly created JAR file.
      Returns:
      File The file that will eventually be created. Note that the main work is done using a separate thread, hence the method returns BEFORE the JAR file is actually created.
      See Also:
    • createManifest

      public static Manifest createManifest(String classpath, String mainclass)
      From LaunchBuilder save jar Creates a Manifest for a JAR file with the given parameters
      Parameters:
      classpath - String
      mainclass - String
      Returns:
      Manifest
    • extract

      public static File extract(File source, String filename, String destination)
      From ResourceLoader Extracts a given file from a compressed (ZIP, JAR or TRZ) file
      Parameters:
      source - File The compressed file to extract the file from
      filename - String The path of the file to extract
      destination - String The full (or relative to whatever the current user directory is) path where to save the extracted file
      Returns:
      File The extracted file, null if failed
    • copy

      public static boolean copy(File source, File target)
      Copies a file. If the target file exists, it will be overwritten.
      Parameters:
      source - File The file to copy
      target - File destination file
      Returns:
      boolean true if successful
    • compress

      public static boolean compress(File source, File target, Manifest manifest)
      Compresses a directory into a single JAR or ZIP file. If the target file exists it will be overwritten.
      Parameters:
      source - File The directory to compress
      target - File The output file
      manifest - Manifest The manifest (in case of a JAR file)
      Returns:
      boolean
    • compress

      public static boolean compress(ArrayList<File> sources, File target, Manifest manifest)
      Compresses a list of files and/or directories into a single JAR or ZIP file. All files/dirs must be in the same directory. If the target file exists it will be overwritten.
      Parameters:
      sources - ArrayList The list of files or directories to compress
      target - File The output file
      manifest - Manifest The manifest (in case of a JAR file)
      Returns:
      boolean
    • remove

      public static boolean remove(File directory)
      Completely removes a directory (without warning!)
      Parameters:
      directory - File The directory to delete
    • getContents

      public static Collection<File> getContents(File directory)
      Returns all the files under a given directory
      Parameters:
      directory - File
      Returns:
      ArrayList
    • unzip

      public static boolean unzip(File source, File targetDirectory)
      Uncompresses a ZIP or JAR file into a given directory. Duplicated files will be overwritten.
      Parameters:
      source - File The compressed file to uncompress
      targetDirectory - File The target directory
      Returns:
      boolean
      See Also:
    • unzipNoOverwrite

      public static boolean unzipNoOverwrite(File source, File targetDirectory)
      Uncompresses a ZIP or JAR file into a given directory. Duplicated files will NOT be overwriten.
      Parameters:
      source - File The compressed file to uncompress
      targetDirectory - File The target directory
      Returns:
      boolean
      See Also:
    • unzipWithAWarning

      public static boolean unzipWithAWarning(File source, File targetDirectory)
      Uncompresses a ZIP or JAR file into a given directory. The system will issue a warning before duplicating existing files.
      Parameters:
      source - File The compressed file to uncompress
      targetDirectory - File The target directory
      Returns:
      boolean
      See Also:
    • unzipWithWarning

      public static List<File> unzipWithWarning(InputStream zipStream, File targetDirectory, JLabel label, String prefix)
      Uncompresses a ZIP or JAR file into a given directory. policy.value indicates what to do on duplicated files.
      Parameters:
      zipStream - The InputStream to read from
      targetDirectory - File The target directory
      label - An optional JLabel to display messages
      prefix - A prefix to add to the extracted file in order to create the message
      Returns:
      java.util.List the set of files extracted, null if cancelled