0

jar ファイル自体から、jar ファイル内にパッケージ化されたバッチ ファイルを呼び出そうとしています。これが可能かどうかは完全にはわかりませんが、誰かが知っているとしたら、ここにいる誰かだと思いました。

import java.io.IOException;
import java.net.URISyntaxException;
public class FileLocationFinder {

    public static void main(String[] args) throws IOException, URISyntaxException {
        String curdir = System.getProperty("user.dir");
        System.out.println("The User.dir = " + curdir);
        File newFile = new File (FileLocationFinder.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
        String strNewFile = newFile.toString();
        System.out.println("The New Path = " + strNewFile);     
        String abPath = new File(".").getAbsolutePath();
        System.out.println("The absolutePath = " + abPath);
        String conPath = new File(".").getCanonicalPath();
        System.out.println("The Canonical Path = " + conPath);
        runDir(strNewFile);
    }
    public static void runDir(String dir){
        final int exitVal;
        final Process dirprocess;
        try {
            System.out.println("Running from curdir");
            dirprocess = Runtime.getRuntime().exec(dir + "\\" + "SomeBatch.bat");
            try {
                exitVal = dirprocess.waitFor();
                if(exitVal == 0){
                    System.out.println("Dir worked");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

SomeBatch.bat の中身は

echo I was called

これは、コンパイルして実行すると、期待どおりに実行されます。しかし、jarファイルを作成すると、多くのエラーが発生します。

4

1 に答える 1