如何在nashorn javascript中使用Paths(java.nio.file.Paths)

时间:2019-05-15 12:34:49

标签: javascript java nashorn

我有一条路径(例如:"C:/Users/aaa/bbb/ccc/"),我的目标是获取父路径(即"C:/Users/aaa/bbb/"

为此,我尝试使用java.nio.file.Paths(在Java中工作),然后尝试将其转换为nashorn js:

import java.nio.file.Paths;
public class ReadPropertiesFile {
    public static void main(String[] args) {
            String myPath = "C:/Users/aaa/bbb/ccc/";
            System.out.println(Paths.get(myPath).getParent());
    }
}

Nashorn脚本:

var testPath = "C:/Users/aaa/bbb/ccc/";
var Paths = java.import("java.nio.file.Paths");
try{
    var test = Paths.get(testPath).getParent();
                            console.log("++++++MyFINALPATH+++++"+test); 
}catch (e){                         console.log("++++++MyFINALPATH+++++"+e);
}

预期输出:C:/Users/aaa/bbb/

  

错误-   TypeError:Paths.get(...)。getParent不是函数

1 个答案:

答案 0 :(得分:0)

您需要改用它:

var Paths = Java.type("java.nio.file.Paths");

https://docs.oracle.com/javase/9/scripting/using-java-scripts.htm#JSJSG117