0

I generated (in eclipse) test.jar file from a simple code:

package testJawka;

public class VarArgsDemo {

  public static void main(String[] args) {
      if (args.equals("1")){
            System.out.println("test 1");
        } else if (args.equals("2")){
            System.out.println("test 2");
        }
  }
}

and in command line (win7) was result like that:

C:\DATA>java -jar test.jar -D 1
Exception in thread "main" java.lang.UnsupportedClassVersionError: testJawka/VarArgsDemo : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: testJawka.VarArgsDemo. Program will exit.

Generally, i want check how does it work i mean the jar file witk some attributes. Maybe it's something wrong with jar file. Does anyone can help me?

Wojtek
  • 27
  • 1
  • 2
  • 10
  • 2
    `if (args.equals("1"))` <-- can never be true. `args` is a `String[]`, `"1"` is a `String`. Do you mean to check for the first argument? – fge Feb 28 '14 at 11:04
  • 1
    Please see this http://stackoverflow.com/questions/10382929/unsupported-major-minor-version-51-0 answer for your question – Ilya Melnik Feb 28 '14 at 11:06
  • It's new for me, In this example i want run programm with attributes, so if i set '1' should be display "test 1" and if '2' then "test 2" ? – Wojtek Feb 28 '14 at 11:21

2 Answers2

1

Unsupported major.minor version 51.0

mean, source code compiled with higher JDK version, ant it's ran by lower JDK version.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
0

I have two comments for your code:

  1. As fge has said, args in Main function has the type of array, not String, you should access the args like this, args_0 = args[0], something like this.

  2. I didn't check that, but I think that Java will regard '-D' prompt as an args, in other words, the args in your program should has two parameters, args[0] = "-D" and args[1] = "1".

Comment here:

package testJawka;

public class VarArgsDemo {

public static void main(String[] args) {

    if (args.length > 0) {
        if (args[0].equals("1")) {
            System.out.println("test 1");
        } else if (args[0].equals("2")) {
            System.out.println("test 2");
        }
    }

}
}

And use the command "java -jar test.jar 1", should work, try it.

Just in case you may have the path and package problems: Suppose you put your project "testJawka" under directory "C:\tmp". You should access in "c:\tmp\testJawka", open comman line:

c:\tmp\testJawka>javac VarArgsDemo.java

This should generate the .class file "VarArgsDemo.class" under "c:\tmp\testJawka".

Then you can just execute the program in command line, but remember change your directory to "c:\tmp", in command line:

c:\tmp>java testJawka.VarArgsDemo 1

If you want to generate the executable jar file, go to directory "c:\tmp", in command line:

c:\tmp>jar -cef testJawka.VarArgsDemo test.jar testJawka

This will generate the test.jar file under directory "c:\tmp".

Then in directory "c:\tmp", execute the jar file in command line:

java -jar test.jar 1

Hope it will be useful for you.