0

My code is:

public class StanfordTagger {

    public static void main(String[] args)throws IOException, ClassNotFoundException
    {             
        System.out.println("Starting Stanford Part of Speech Tagger");
        //get file name with path from user
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the path and filename to tag: ");
        String fileName = keyboard.nextLine();

        String line = null;
        // FileReader reads text files in the default encoding.
        FileReader fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        String newFileName = fileName.replace(".txt", "POSTagger.txt");
        //create a FileWriter using the new path and file
        FileWriter fileWriter = new FileWriter(newFileName);
        // Always wrap FileWriter in BufferedWriter.
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);


        /* creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution 
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);*/


        // Initialize the tagger with a model
        MaxentTagger tagger = new MaxentTagger(
        "G:\\Natural Language Final Project\\taggers\\english-left3words-distsim.tagger");

        while((line = bufferedReader.readLine()) != null) {
            System.out.println(tagger.tagString(line));
            bufferedWriter.write(tagger.tagString(line)+"\n");
        }      

        // Always close files.
        bufferedReader.close(); 
        bufferedWriter.flush();
        bufferedWriter.close();

    }
}

And the error when I run is:

Enter the path and filename to tag: G:\Natural Language Final Project\tasks_1-20_v1-2\en\qa6_yes-no-questions_test.txt
Exception in thread "main" java.lang.UnsupportedClassVersionError: edu/stanford/nlp/tagger/maxent/MaxentTagger : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at StanfordTagger.main(StanfordTagger.java:78)
Java Result: 1
BUILD SUCCESSFUL (total time: 39 seconds)

Does anyone know what I need to do to fix this?

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
David Farthing
  • 237
  • 2
  • 13
  • "When JVM tries to load a class and found that class file version is not supported it throws UnSupportedClassVersionError and it generally occurs if a higher JDK version is used to compile the source file and a lower JDK version is used to run the program." – Rosendo Ropher May 26 '16 at 15:32
  • 3
    Try to take a look at this post http://stackoverflow.com/questions/10382929/how-to-fix-java-lang-unsupportedclassversionerror-unsupported-major-minor-versi – Yantes May 26 '16 at 15:35

2 Answers2

0

You use a newer JVM.

New JVMs can support loading (most) older class versions. Older JVMs can't load class versions that didn't exist when the JVM was created.

From the class version (52) you are trying to run code that was complied in a Java 8 environment on a JVM that's Java 7 or lower.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

"When JVM tries to load a class and found that class file version is not supported it throws UnSupportedClassVersionError and it generally occurs if a higher JDK version is used to compile the source file and a lower JDK version is used to run the program."

Sure that you are compiling your code with the same (or minor) Java version to run it.

You are trying to run a Java 8 (for example) compiled code on a Java 6 (for example) JVM.

Rosendo Ropher
  • 496
  • 8
  • 21