我正在尝试使用maven编译一些Java代码以在Spark上运行。除了辅助类之外,我的所有代码都在我的主方法中。我觉得问题是我没有在正确的位置定义类(class)。我尝试将它放在 main 方法中,在具有 main 方法的类之后和类之前。我需要一个完整的单独文件来放置此类吗?当我运行时
mvn package
我得到:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler- plugin:2.0.2:compile (default-compile) on project simple-project: Compilation failure: Compilation failure:
[ERROR] /home/peter/Desktop/spark/spark-1.1.0/sent-svm/src/main/java/GenClassifiers.java: [10,7] class, interface, or enum expected
[ERROR]
[ERROR] /home/peter/Desktop/spark/spark-1.1.0/sent-svm/src/main/java/GenClassifiers.java:[12,11] class, interface, or enum expected
[ERROR]
[ERROR] /home/peter/Desktop/spark/spark-1.1.0/sent-svm/src/main/java/GenClassifiers.java:[14,9] class, interface, or enum expected
[ERROR]
[ERROR] /home/peter/Desktop/spark/spark-1.1.0/sent-svm/src/main/java/GenClassifiers.java:[15,4] class, interface, or enum expected
[ERROR]
[ERROR] /home/peter/Desktop/spark/spark-1.1.0/sent-svm/src/main/java/GenClassifiers.java:[17,11] class, interface, or enum expected
[ERROR]
[ERROR] /home/peter/Desktop/spark/spark-1.1.0/sent-svm/src/main/java/GenClassifiers.java:[19,4] class, interface, or enum expected
[ERROR]
[ERROR] /home/peter/Desktop/spark/spark-1.1.0/sent-svm/src/main/java/GenClassifiers.java:[21,11] class, interface, or enum expected
[ERROR]
[ERROR] /home/peter/Desktop/spark/spark-1.1.0/sent-svm/src/main/java/GenClassifiers.java:[23,4] class, interface, or enum expected
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project simple-project: Compilation failure
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilde r.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilde r.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.jav a:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:622)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:516)
at org.apache.maven.plugin.CompilerMojo.execute(CompilerMojo.java:114)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java: 101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
... 19 more
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
这是我当前的代码,尝试将类放在前面,然后是其余代码:
/* SimpleApp.java */
import org.apache.spark.api.java.*;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.Function;
import cmu.arktweetnlp.Twokenize;
import java.util.*;
import java.io.*;
public Class Tweet {
public Tweet(List<String> tokens, String sentiment) {
private List<String> tweetTokens = tokens;
private String label = sentiment;
}
public List<String> getTokens() {
return tweetTokens;
}
public String getLabel() {
return label;
}
}
public class GenClassifiers {
public static void main(String[] args) {
最佳答案
您有一个拼写错误:
public Class Tweet {
而不是
public class Tweet {
此外,在同一个 Java 文件中不能定义多个公共(public)类。公共(public)类应该与文件名相同。因此,您需要在自己的文件中定义 Tweet
和 GenClassifiers
。
关于java - 使用 Maven 打包 Java 代码时出现类、接口(interface)或枚举预期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27385808/