英文:
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("externs.js",
"function textDiv(text){};");
// The dummy input name "input.js" is used here so that any warnings or
// errors will cite line numbers in terms of input.js.
SourceFile input = SourceFile.fromCode("input.js", 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() > 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);
}
}
--------------------------------error--error--------------------------------
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$$$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
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
专注分享java语言的经验与见解,让所有开发者获益!
评论