Tool: Copy jar from one folder to another, and jar to another

Source: Internet
Author: User

Tool: Copy jar from one folder to another, and jar to another

Tool class: Copy jar from one folder to another

If you need a friend, you can try it. It's nice. Sometimes it's really necessary!

Requirement: when we get a maven project, the jar package of the maven project uses pom. for xml file management, we need jar in maven at this time (many, the directory is very complex, it is difficult to manually copy)

There are two methods:

Traditional method 1. Find the location where the local maven is stored and copy the jar package one by one. (It takes one hour to copy the jar package in half)

Convenient way 2. Use java to write a tool to help us do this, convenient and quick (20 s)

As needed, I have the tool class here, which is described below:

The usage is simple. In my main method, you only need to give two parameters: the path of the source folder and the path of the target folder. Run the command to complete all jar copies.

Technologies used: traditional java I/O operations


1. The destination folder path must be created first.

Source code:

Package cc. makalu. file; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException;/***** @ ClassName: downJarFromFile * @ Package: cc. makalu. file * @ Description: copy the jar package from the file * @ author: ljl * @ version: v0.1 * @ data: 2:25:38, January 1, January 16, 2018 remarks: */public class downJarFromFile {public static void readFile (String resourcePath, String targetFile, String fileName) throws FileNotFoundException {// C: \ Users \ Administrator \. m2 \ repository \ anlr \ 2.7.7 \ antlr-2.7.7.jarString targetPath = targetFile + "\" + fileName; targetPath = targetPath. replaceAll ("\\\\", "/"); // System. out. println (targetPath); // F: \ jar file \ antlr-2.7.7.jarresourcePath = resourcePath. replaceAll ("\\\\", "/"); // System. out. println (resourcePath); FileInputStream in = n Ew FileInputStream (resourcePath); FileOutputStream out = new FileOutputStream (targetPath); byte [] buffer = new byte [2097152]; try {while (in. read (buffer ))! =-1) {out. write (buffer) ;}} catch (IOException e) {e. printStackTrace () ;}} public static boolean copyJarFromFile (String resourceFile, String targetFile) throws FileNotFoundException {boolean flag = false; File file = new File (resourceFile); if (! File. isDirectory () {String fileName = file. getName (); String absolutepath = file. getAbsolutePath (); System. out. println ("file"); System. out. println ("path =" + file. getPath (); System. out. println ("absolutepath =" + absolutepath); System. out. println ("name =" + fileName); // obtain the file suffix if (fileName. indexOf (". ")! =-1) {String xt = fileName. substring (fileName. indexOf (". "), fileName. length (); if (xt. contains (". jar ") & xt. endsWith (". jar ") {readFile (absolutepath, targetFile, fileName) ;}} else if (file. isDirectory () {System. out. println ("folder"); String [] filelist = file. list (); for (int I = 0; I <filelist. length; I ++) {File readfile = new File (resourceFile + "\" + filelist [I]); if (! Readfile. isDirectory () {String fileName = readfile. getName (); String absolutepath = readfile. getAbsolutePath (); System. out. println ("path =" + readfile. getPath (); System. out. println ("absolutepath =" + readfile. getAbsolutePath (); System. out. println ("name =" + readfile. getName (); // get the file suffix if (fileName. lastIndexOf (". ")! =-1) {String xt = fileName. substring (fileName. indexOf (". "), fileName. length (); if (xt. contains (". jar ") & xt. endsWith (". jar ") {readFile (absolutepath, targetFile, fileName) ;}} else if (readfile. isDirectory () {copyJarFromFile (resourceFile + "\" + filelist [I], targetFile) ;}}return flag;} public static void main (String [] args) {try {copyJarFromFile ("C :\\ Users \ Administrator \\. m2 \ repository "," F: \ jar file ");} catch (FileNotFoundException e) {e. printStackTrace ();}}}

Run the following command:



Finally, all the jar files in the C: \ Users \ Administrator \. m2 \ repository directory are copied to the F: \ jar file directory.

It takes tens of seconds to complete things that can only be completed in an hour !!! It feels great.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.