集册 Java实例教程 将给定的jar文件URL解析为Jar File对象。

将给定的jar文件URL解析为Jar File对象。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

509
将给定的jar文件URL解析为Jar File对象。
//NowJava.com 提供


//package com.nowjava;


import java.io.IOException;


import java.net.URI;

import java.net.URISyntaxException;


import java.util.jar.JarFile;


public class Main {

    public static void main(String[] argv) throws Exception {

        String jarFileUrl = "nowjava.com";

        System.out.println(getJarFile(jarFileUrl));

    }


    /** URL prefix for loading from the file system: "file:" */

    public static final String FILE_URL_PREFIX = "file:";


    /**

     * Resolve the given jar file URL into a JarFile object.

     */

    protected static JarFile getJarFile(String jarFileUrl)

            throws IOException {/*来自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m*/

        if (jarFileUrl.startsWith(FILE_URL_PREFIX)) {

            try {

                return new JarFile(

                        new URI(jarFileUrl.replaceAll(" ", "%20"))

                                .getSchemeSpecificPart());

            } catch (URISyntaxException ex) {

                // Fallback for URLs that are not valid URIs (should hardly ever happen).

  
展开阅读全文