How to get the current working directory in Java?

前端 未结 22 3448
时光说笑
时光说笑 2020-11-21 07:16

I want to access my current working directory using java.

My code :

 String current = new java.io.File( \".\" ).getCanonicalPath();
        System.ou         


        
22条回答
  •  时光取名叫无心
    2020-11-21 07:55

    This is my silver bullet when ever the moment of confusion bubbles in.(Call it as first thing in main). Maybe for example JVM is slipped to be different version by IDE. This static function searches current process PID and opens VisualVM on that pid. Confusion stops right there because you want it all and you get it...

      public static void callJVisualVM() {
        System.out.println("USER:DIR!:" + System.getProperty("user.dir"));
        //next search current jdk/jre
        String jre_root = null;
        String start = "vir";
        try {
            java.lang.management.RuntimeMXBean runtime =
                    java.lang.management.ManagementFactory.getRuntimeMXBean();
            String jvmName = runtime.getName();
            System.out.println("JVM Name = " + jvmName);
            long pid = Long.valueOf(jvmName.split("@")[0]);
            System.out.println("JVM PID  = " + pid);
            Runtime thisRun = Runtime.getRuntime();
            jre_root = System.getProperty("java.home");
            System.out.println("jre_root:" + jre_root);
            start = jre_root.concat("\\..\\bin\\jvisualvm.exe " + "--openpid " + pid);
            thisRun.exec(start);
        } catch (Exception e) {
            System.getProperties().list(System.out);
            e.printStackTrace();
        }
    }
    

提交回复
热议问题