Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.toImmutableList()Ljava/util/stream/Collector;

huangapple 未分类评论48阅读模式
英文:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.toImmutableList()Ljava/util/stream/Collector;

问题

我在Google中使用了Closure Compiler。我在这方面是个初学者。当我运行我的代码时,出现了一个错误。
我想知道是什么原因或者是否有使用的示例。

我的代码:

public class JsClosureCompiler {
    public static String compileJs(String code){

        Compiler compiler = new Compiler();

        CompilerOptions options = new CompilerOptions();
        // 这里使用了简单模式,但也可以设置其他选项。
        CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

        // 要获取完整的externs集合,应该在这里使用CompilerRunner.getDefaultExterns()中的逻辑。
        SourceFile extern = SourceFile.fromCode("externs.js",
                "function textDiv(text){};");

        // 这里使用虚拟的输入名称"input.js",以便任何警告或错误会引用input.js中的行号。
        SourceFile input = SourceFile.fromCode("input.js", code);

        // compile() 返回一个Result,但这里不需要。
        compiler.compile(extern, input, options);

        // 编译器负责生成编译后的代码;无法通过Result访问它。
        if(compiler.getErrorCount() > 0){
            StringBuilder erroInfo = new StringBuilder();
            for(JSError jsError: compiler.getErrors()) {
                erroInfo.append(jsError.toString());
            }
        }
        return compiler.toSource();
    }

    public static void main(String[] args) {
        String code = "function makeNoteDom(noteTitle, noteContent, noteContainer) {\n" +
                "  // Create DOM structure to represent the note.\n" +
                "  var headerElement = textDiv(noteTitle);\n" +
                "  var contentElement = textDiv(noteContent);\n" +
                "\n" +
                "  var newNote = document.createElement('div');\n" +
                "  newNote.appendChild(headerElement);\n" +
                "  newNote.appendChild(contentElement);\n" +
                "\n" +
                "  // Add the note's DOM structure to the document.\n" +
                "  noteContainer.appendChild(newNote);\n" +
                "}\n" +
                "\n" +
                "/**\n" +
                " * Iterates over a list of note data objects and creates a DOM\n" +
                " */\n" +
                "function makeNotes(data, noteContainer) {\n" +
                "  for (var i = 0; i < data.length; i++) {\n" +
                "    makeNoteDom(data[i].title, data[i].content, noteContainer);\n" +
                "  }\n" +
                "}\n" +
                "\n" +
                "function main() {\n" +
                "  var noteData = [\n" +
                "      {title: 'Note 1', content: 'Content of Note 1'},\n" +
                "      {title: 'Note 2', content: 'Content of Note 2'}];\n" +
                "  var noteListElement = document.getElementById('notes');\n" +
                "  makeNotes(noteData, noteListElement);\n" +
                "}\n" +
                "\n" +
                "main();";
        String s = compileJs(code);
        System.out.println(s);
    }
}

--------------------------------错误--错误--------------------------------

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.toImmutableList()Ljava/util/stream/Collector;
	at com.google.javascript.jscomp.deps.DependencyInfo$Require.asSymbolList(DependencyInfo.java:60)
	at com.google.javascript.jscomp.deps.DependencyInfo$Base.getRequiredSymbols(DependencyInfo.java:163)
	at com.google.javascript.jscomp.Compiler.findModulesFromInput(Compiler.java:1901)
	at com.google.javascript.jscomp.Compiler.findModulesFromEntryPoints(Compiler.java:1857)
	at com.google.javascript.jscomp.Compiler.parseInputs(Compiler.java:1666)
	at com.google.javascript.jscomp.Compiler.parseForCompilationInternal(Compiler.java:939)
	at com.google.javascript.jscomp.Compiler.lambda$parseForCompilation$4(Compiler.java:922)
	at com.google.javascript.jscomp.CompilerExecutor$2.call(CompilerExecutor.java:102)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
英文:

I'm using the Closure Compiler in Google. I'm a beginner at this. When I run my code, I get an error.<br>
I want to know what is the reason or is there any demo used.

my code:

public class JsClosureCompiler {
    public static String compileJs(String code){

        Compiler compiler = new Compiler();

        CompilerOptions options = new CompilerOptions();
        // Simple mode is used here, but additional options could be set, too.
        CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

        // To get the complete set of externs, the logic in
        // CompilerRunner.getDefaultExterns() should be used here.
        SourceFile extern = SourceFile.fromCode(&quot;externs.js&quot;,
                &quot;function textDiv(text){};&quot;);

        // The dummy input name &quot;input.js&quot; is used here so that any warnings or
        // errors will cite line numbers in terms of input.js.
        SourceFile input = SourceFile.fromCode(&quot;input.js&quot;, code);

        // compile() returns a Result, but it is not needed here.
        compiler.compile(extern, input, options);

        // The compiler is responsible for generating the compiled code; it is not
        // accessible via the Result.
        if(compiler.getErrorCount() &gt; 0){
            StringBuilder erroInfo = new StringBuilder();
            for(JSError jsError: compiler.getErrors()) {
                erroInfo.append(jsError.toString());
            }
        }
        return compiler.toSource();
    }

    public static void main(String[] args) {
        String code = &quot;function makeNoteDom(noteTitle, noteContent, noteContainer) {\n&quot; +
                &quot;  // Create DOM structure to represent the note.\n&quot; +
                &quot;  var headerElement = textDiv(noteTitle);\n&quot; +
                &quot;  var contentElement = textDiv(noteContent);\n&quot; +
                &quot;\n&quot; +
                &quot;  var newNote = document.createElement(&#39;div&#39;);\n&quot; +
                &quot;  newNote.appendChild(headerElement);\n&quot; +
                &quot;  newNote.appendChild(contentElement);\n&quot; +
                &quot;\n&quot; +
                &quot;  // Add the note&#39;s DOM structure to the document.\n&quot; +
                &quot;  noteContainer.appendChild(newNote);\n&quot; +
                &quot;}\n&quot; +
                &quot;\n&quot; +
                &quot;/**\n&quot; +
                &quot; * Iterates over a list of note data objects and creates a DOM\n&quot; +
                &quot; */\n&quot; +
                &quot;function makeNotes(data, noteContainer) {\n&quot; +
                &quot;  for (var i = 0; i &lt; data.length; i++) {\n&quot; +
                &quot;    makeNoteDom(data[i].title, data[i].content, noteContainer);\n&quot; +
                &quot;  }\n&quot; +
                &quot;}\n&quot; +
                &quot;\n&quot; +
                &quot;function main() {\n&quot; +
                &quot;  var noteData = [\n&quot; +
                &quot;      {title: &#39;Note 1&#39;, content: &#39;Content of Note 1&#39;},\n&quot; +
                &quot;      {title: &#39;Note 2&#39;, content: &#39;Content of Note 2&#39;}];\n&quot; +
                &quot;  var noteListElement = document.getElementById(&#39;notes&#39;);\n&quot; +
                &quot;  makeNotes(noteData, noteListElement);\n&quot; +
                &quot;}\n&quot; +
                &quot;\n&quot; +
                &quot;main();&quot;;
        String s = compileJs(code);
        System.out.println(s);
    }
}

--------------------------------error--error--------------------------------

Exception in thread &quot;main&quot; java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.toImmutableList()Ljava/util/stream/Collector;
	at com.google.javascript.jscomp.deps.DependencyInfo$Require.asSymbolList(DependencyInfo.java:60)
	at com.google.javascript.jscomp.deps.DependencyInfo$Base.getRequiredSymbols(DependencyInfo.java:163)
	at com.google.javascript.jscomp.Compiler.findModulesFromInput(Compiler.java:1901)
	at com.google.javascript.jscomp.Compiler.findModulesFromEntryPoints(Compiler.java:1857)
	at com.google.javascript.jscomp.Compiler.parseInputs(Compiler.java:1666)
	at com.google.javascript.jscomp.Compiler.parseForCompilationInternal(Compiler.java:939)
	at com.google.javascript.jscomp.Compiler.lambda$parseForCompilation$4(Compiler.java:922)
	at com.google.javascript.jscomp.CompilerExecutor$2.call(CompilerExecutor.java:102)
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
	at java.util.concurrent.FutureTask.run(FutureTask.java)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

答案1

得分: 0

放置Guava到pom.xml文件中:

<dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>22.0</version>
</dependency>
英文:

put guava into pom.xml

&lt;dependency&gt;
   &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
   &lt;artifactId&gt;guava&lt;/artifactId&gt;
   &lt;version&gt;22.0&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年5月29日 16:58:56
  • 转载请务必保留本文链接:https://java.coder-hub.com/62082230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定