0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pathからクラスのロード

Last updated at Posted at 2017-01-14

#概要
Pathが示すclassファイルをロードする。
#コード

ClassLoaderFromPath.java
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
public class ClassLoaderFromPath extends ClassLoader{
    private final ClassLoader parent;
    public ClassLoaderFromPath(ClassLoader parent) {
        super(parent);
        this.parent = parent;
    }

    public Class loadClassFile(Path path) throws IOException{
        byte[] data = Files.readAllBytes(path);
        return defineClass(null,data,0,data.length);
    }
}

2017/01/14-20:35

以上のままではstatic initializerが呼ばれないため修正

ClassLoaderFromPath.java
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
public class ClassLoaderFromPath extends ClassLoader{
    private final ClassLoader parent;
    public ClassLoaderFromPath(ClassLoader parent) {
        super(parent);
        this.parent = parent;
    }

    public Class loadClassFile(Path path) throws IOException,ClassNotFoundException{
        byte[] data = Files.readAllBytes(path);
        return Class.forName(defineClass(null,data,0,data.length).getName());
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?