Example usage for java.net URL getFile

List of usage examples for java.net URL getFile

Introduction

In this page you can find the example usage for java.net URL getFile.

Prototype

public String getFile() 

Source Link

Document

Gets the file name of this URL .

Usage

From source file:MainClass.java

public static void main(String args[]) {
    String name = "http://urlWithClassName";
    try {//from   www  .j ava 2 s  .c  om
        if (!name.endsWith(".class")) {
            System.err.println("That doesn't look like a byte code file!");
            return;
        }
        URL u = new URL(name);
        URLClassLoader ucl = new URLClassLoader(u);

        // parse out the name of the class from the URL
        String s = u.getFile();
        String classname = s.substring(s.lastIndexOf('/'), s.lastIndexOf(".class"));
        Class AppletClass = ucl.loadClass(classname, true);
        Applet apl = (Applet) AppletClass.newInstance();
        JFrame f = new JFrame();
        f.setSize(200, 200);
        f.add("Center", apl);
        apl.init();
        apl.start();
        f.setVisible(true);
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:MinAppletviewer.java

public static void main(String args[]) throws Exception {
    AppSupport theAppSupport = new AppSupport();
    JFrame f = new JFrame();
    URL toload = new URL(args[0]);
    String host = toload.getHost();
    int port = toload.getPort();
    String protocol = toload.getProtocol();
    String path = toload.getFile();
    int join = path.lastIndexOf('/');
    String file = path.substring(join + 1);
    path = path.substring(0, join + 1);/*from   w  w w  .j  ava  2 s. c  o m*/

    theAppSupport.setCodeBase(new URL(protocol, host, port, path));
    theAppSupport.setDocumentBase(theAppSupport.getCodeBase());

    URL[] bases = { theAppSupport.getCodeBase() };
    URLClassLoader loader = new URLClassLoader(bases);
    Class theAppletClass = loader.loadClass(file);
    Applet theApplet = (Applet) (theAppletClass.newInstance());

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    f.add(theApplet, BorderLayout.CENTER);

    theApplet.setStub(theAppSupport);

    f.setSize(200, 200);
    f.setVisible(true);
    theApplet.init();
    theApplet.start();
}

From source file:MainClass.java

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

    ParserGetter kit = new ParserGetter();
    HTMLEditorKit.Parser parser = kit.getParser();

    URL u = new URL("http://www.java2s.com");
    InputStream in = u.openStream();
    InputStreamReader r = new InputStreamReader(in);
    String remoteFileName = u.getFile();
    if (remoteFileName.endsWith("/")) {
        remoteFileName += "index.html";
    }/*from  www .j a v a 2s  .  co  m*/
    if (remoteFileName.startsWith("/")) {
        remoteFileName = remoteFileName.substring(1);
    }
    File localDirectory = new File(u.getHost());
    while (remoteFileName.indexOf('/') > -1) {
        String part = remoteFileName.substring(0, remoteFileName.indexOf('/'));
        remoteFileName = remoteFileName.substring(remoteFileName.indexOf('/') + 1);
        localDirectory = new File(localDirectory, part);
    }
    if (localDirectory.mkdirs()) {
        File output = new File(localDirectory, remoteFileName);
        FileWriter out = new FileWriter(output);
        HTMLEditorKit.ParserCallback callback = new PageSaver(out, u);
        parser.parse(r, callback, false);
    }

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.yahoo.com:80/en/index.html?name=joe#first");
    System.out.println("protocol:" + url.getProtocol());
    System.out.println("prot:" + url.getPort());
    System.out.println("host:" + url.getHost());
    System.out.println("path:" + url.getPath());
    System.out.println("file:" + url.getFile());
    System.out.println("query:" + url.getQuery());
    System.out.println("ref:" + url.getRef());
}

From source file:de.oth.keycloak.InitKeycloakServer.java

public static void main(String[] args) {
    CheckParams checkParams = CheckParams.create(args, System.out, InitKeycloakServer.class.getName());
    if (checkParams == null) {
        System.exit(1);//from   w w  w  .  j a  v  a 2 s  .c  om
    }
    try {
        String server = checkParams.getServer();
        String realm = checkParams.getRealm();
        String user = checkParams.getUser();
        String pwd = checkParams.getPwd();
        String clientStr = checkParams.getClient();
        String secret = checkParams.getSecret();
        String initFileStr = checkParams.getInitFile();
        File initFile = new File(initFileStr);
        if (!initFile.isFile()) {
            URL url = InitKeycloakServer.class.getClassLoader().getResource(initFileStr);
            if (url != null) {
                initFile = new File(url.getFile());
                if (!initFile.isFile()) {
                    log.error("init file does not exist: " + initFile);
                    System.exit(1);
                }
            } else {
                log.error("init file does not exist: " + initFile);
                System.exit(1);
            }
        }
        Keycloak keycloak = (secret == null) ? Keycloak.getInstance(server, realm, user, pwd, clientStr)
                : Keycloak.getInstance(server, realm, user, pwd, clientStr, secret);

        ObjectMapper mapper = new ObjectMapper();
        RealmsConfig realmsConfig = mapper.readValue(initFile, RealmsConfig.class);

        if (realmsConfig != null) {
            List<RealmConfig> realmList = realmsConfig.getRealms();
            if (realmList == null || realmList.isEmpty()) {
                log.error("no realms config found 1");
                return;
            }
            for (RealmConfig realmConf : realmList) {
                addRealm(keycloak, realmConf);
            }
        } else {
            log.error("no realms config found 2");
        }
    } catch (Exception e) {
        log.error(e.getClass().getName() + ": " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:GetURLParts.java

public static void main(String args[]) {

    try {//from  w w w. j  av a  2s.c  o  m
        URL u = new URL("http://www.java2s.com");
        System.out.println("The URL is " + u);
        System.out.println("The protocol part is " + u.getProtocol());
        System.out.println("The host part is " + u.getHost());
        System.out.println("The port part is " + u.getPort());
        System.out.println("The file part is " + u.getFile());
        System.out.println("The ref part is " + u.getRef());
    } // end try
    catch (MalformedURLException e) {
        System.err.println("not a URL I understand.");
    }

}

From source file:de.ipbhalle.metfusion.utilities.chemaxon.ChemAxonUtilities.java

public static void main(String[] args) {
    ChemAxonUtilities cau = new ChemAxonUtilities(true);
    URL url = cau.getClass().getResource("fcfp.xml");
    System.out.println("file -> " + url.getFile());
    System.out.println("path -> " + url.getPath());

    ECFP one = cau.generateECFPFromName("O=P(c1ccccc1)(c2ccccc2)C");
    ECFP two = cau.generateECFPFromName("O=P4(C1C3C2CC1C4C23)c5ccccc5");
    System.out.println("tan test -> " + (1 - one.getTanimoto(two)));

    ECFPParameters params = new ECFPParameters(new File(url.getFile()));
    ECFPFeatureLookup lookup = new ECFPFeatureLookup(params);
    MolHandler mh = null;//from   w  ww  .j  av  a2  s . com
    try {
        mh = new MolHandler("O=P(c1ccccc1)(c2ccccc2)C");
        Molecule m = mh.getMolecule();
        lookup.processMolecule(m);
        int[] arr = one.toIntArray();
        for (int i = 0; i < arr.length; i++) {
            for (ECFPFeature f : lookup.getFeaturesFromIdentifier(arr[i])) {
                //System.out.println(f.getSubstructure().toFormat("SMARTS"));
                try {
                    System.out.println(MolExporter.exportToFormat(f.getSubstructure(), "SMARTS"));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    } catch (MolFormatException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
    try {
        Fingerprinter fp = new Fingerprinter();

        IAtomContainer ac1 = sp.parseSmiles("O=P(c1ccccc1)(c2ccccc2)C");
        IAtomContainer ac2 = sp.parseSmiles("O=P4(C1C3C2CC1C4C23)c5ccccc5");
        float tan = Tanimoto.calculate(fp.getFingerprint(ac1), fp.getFingerprint(ac2));
        System.out.println("CDK tanimoto -> " + tan);
    } catch (InvalidSmilesException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CDKException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ChemAxonUtilities cau2 = new ChemAxonUtilities(false);
    ECFP test = cau2.generateECFPFromMol(new File("/vol/massbank/Cache/PB/mol/PB000122.mol"));
    ECFP test2 = cau2.generateECFPFromMol(new File("/vol/massbank/Cache/PB/mol/PB000126.mol"));
    System.out.println("tanimoto dissimilarity -> " + test.getTanimoto(test2));
    System.out.println("tanimoto similarity -> " + (1 - test.getTanimoto(test2)));
}

From source file:de.jwi.ftp.FTPUploader.java

public static void main(String[] args) throws Exception {
    URL url = new URL("ftp://ftp:none@localhost/tmp");
    String protocol = url.getProtocol();

    String host = url.getHost();//from  w w w.j a v a2  s .c o  m
    String userInfo = url.getUserInfo();
    String path = url.getPath();
    String file = url.getFile();

    File f = new File("D:/temp/out");
    List l = new ArrayList();
    l.add(f);
    String s = upload(url, l);
    System.out.println(s);
    int x = 5;
}

From source file:GetURL.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    InputStream urlstream = url.openStream();
    byte[] buffer = new byte[0];
    byte[] chunk = new byte[4096];
    int count;/*from w w w. j av a2 s.  co  m*/

    while ((count = urlstream.read(chunk)) >= 0) {
        byte[] t = new byte[buffer.length + count];
        System.arraycopy(buffer, 0, t, 0, buffer.length);
        System.arraycopy(chunk, 0, t, buffer.length, count);
        buffer = t;
    }

    String filename = (url.getFile()).replace('/', File.separatorChar);
    File f1 = new File(filename);
    filename = f1.getName();
    FileOutputStream f = null;
    f = new FileOutputStream(filename);
    f.write(buffer);
    f.close();
}

From source file:LauncherBootstrap.java

/**
 * The main method./*from   ww w  . j a  v a2  s  . com*/
 *
 * @param args command line arguments
 */
public static void main(String[] args) {

    try {

        // Try to find the LAUNCHER_JAR_FILE_NAME file in the class
        // loader's and JVM's classpath.
        URL coreURL = LauncherBootstrap.class.getResource("/" + LauncherBootstrap.LAUNCHER_JAR_FILE_NAME);
        if (coreURL == null)
            throw new FileNotFoundException(LauncherBootstrap.LAUNCHER_JAR_FILE_NAME);

        // Coerce the coreURL's directory into a file
        File coreDir = new File(URLDecoder.decode(coreURL.getFile())).getCanonicalFile().getParentFile();

        // Try to find the LAUNCHER_PROPS_FILE_NAME file in the same
        // directory as this class
        File propsFile = new File(coreDir, LauncherBootstrap.LAUNCHER_PROPS_FILE_NAME);
        if (!propsFile.canRead())
            throw new FileNotFoundException(propsFile.getPath());

        // Load the properties in the LAUNCHER_PROPS_FILE_NAME 
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream(propsFile);
        props.load(fis);
        fis.close();

        // Create a class loader that contains the Launcher, Ant, and
        // JAXP classes.
        URL[] antURLs = LauncherBootstrap
                .fileListToURLs((String) props.get(LauncherBootstrap.ANT_CLASSPATH_PROP_NAME));
        URL[] urls = new URL[1 + antURLs.length];
        urls[0] = coreURL;
        for (int i = 0; i < antURLs.length; i++)
            urls[i + 1] = antURLs[i];
        ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();
        URLClassLoader loader = null;
        if (parentLoader != null)
            loader = new URLClassLoader(urls, parentLoader);
        else
            loader = new URLClassLoader(urls);

        // Load the LAUNCHER_MAIN_CLASS_NAME class
        launcherClass = loader.loadClass(LAUNCHER_MAIN_CLASS_NAME);

        // Get the LAUNCHER_MAIN_CLASS_NAME class' getLocalizedString()
        // method as we need it for printing the usage statement
        Method getLocalizedStringMethod = launcherClass.getDeclaredMethod("getLocalizedString",
                new Class[] { String.class });

        // Invoke the LAUNCHER_MAIN_CLASS_NAME class' start() method.
        // If the ant.class.path property is not set correctly in the 
        // LAUNCHER_PROPS_FILE_NAME, this will throw an exception.
        Method startMethod = launcherClass.getDeclaredMethod("start", new Class[] { String[].class });
        int returnValue = ((Integer) startMethod.invoke(null, new Object[] { args })).intValue();
        // Always exit cleanly after invoking the start() method
        System.exit(returnValue);

    } catch (Throwable t) {

        t.printStackTrace();
        System.exit(1);

    }

}