2013-07-23 2 views
0

Я пытаюсь запустить эту программу исключительно из командной строки (в этом случае командной строки), но когда я создаю .jar, но я продолжаю получать эту ошибку:Использование Java.util.scanner с командной строкой

Expception in thread "main" java.lang.UnsupportedClassVersionError: Exponentiation: 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: Exponentiation. Program will exit. 

программа:

//import scanner for user input 
import java.util.Scanner; 

public class Exponentiation 
{ 

static long values[]; //array of values to be computed 
static long output[]; //array of computed values 
static int numberExp; //number times the experiment is run 
static int currentExp = 1; //current time the experiment is running 

//main method 
public static void main(String [] args) 
{ 
    //run initial setup and random generating 
    setup(); 

    //run the interative and recursive components until the user specified number of experiments 
    for (currentExp = 1; currentExp <= numberExp; currentExp++) 
    { 
     iterative(); 
     recursive(); 
    } 

} //close main method 

//runs the trail iteratively 
public static void iterative() 
{ 
    //create variables for time 
    long initialTime; 
    long endTime; 
    long diffTime; 

    //record initial time 
    initialTime = System.nanoTime(); 

    //for every value exponetiate 
    for (int counter = 1; counter <= values[0]; counter++) 
    { 
     //raise every value by its opposing value in the array 
     output[counter] = values[counter]^values[(int)values[0] + 1 - counter]; 
    } 

    //recourd end time 
    endTime = System.nanoTime(); 

    //calculate the difference 
    diffTime = endTime - initialTime; 

    //output time values 
    System.out.println(); 
    System.out.println("Experiment " + currentExp + " Iterative:"); 
    System.out.println(initialTime); 
    System.out.println(endTime); 
    System.out.println(diffTime); 
} //close iterative method 

//runs the trial recursively 
public static void recursive() 
{ 
    //create variables for time 
    long initialTime; 
    long endTime; 
    long diffTime; 

    //record initial time 
    initialTime = System.nanoTime(); 

    //for every value exponetiate 
    for (int counter = 1; counter <= values[0]; counter++) 
    { 
     //raise every value by its opposing value in the array using the recursive method 
     output[counter] = recursiveExpo(values[counter],values[(int)values[0] + 1 - counter]); 
    } 

    //recourd end time 
    endTime = System.nanoTime(); 

    //calculate the difference 
    diffTime = endTime - initialTime; 

    //output time values 
    System.out.println(); 
    System.out.println("Experiment " + currentExp + " Recursive:"); 
    System.out.println(initialTime); 
    System.out.println(endTime); 
    System.out.println(diffTime); 
} //close recursive method 

public static long recursiveExpo(long base, long expo) { 
    if (expo == 0) { 
     return 1; 
    } else { 
     return base * recursiveExpo(base, expo - 1); 
    } 
} //close recursiveExpo 

//setup method - runs all setup isolated from the main program 
public static void setup() 
{ 

    int lowRange; //lowest possible random value 
    int highRange; //highest possible random value 
    int numOfTrials; //number of trials/random numbers to be generated 

    //create scanner for user input 
    Scanner input = new Scanner(System.in); 

    //runs the random number generator until a good assortment is found 
    for (boolean goodRandom = false; goodRandom == false;) 
    { 

     //clear values[], just in case 
     values = null; 

     System.out.println("This is the program for Iterative Exponetiation"); 
     System.out.println("Enter your prefered range of numbers"); 

     System.out.println("Lowest possible number"); 
     lowRange = input.nextInt(); //store min random number 

     System.out.println("Highest possible number"); 
     highRange = input.nextInt(); //store max random number 

     System.out.println("Number of trials"); 
     numOfTrials = input.nextInt(); //store number of trials 

     System.out.println("How many times should the experiment be run?"); 
     numberExp = input.nextInt(); //store number of experiments 

     //create array of the correct size and store the size in the 0th element 
     values = new long[numOfTrials+1]; 
     values[0] = numOfTrials; 

     //make the output array the same lenght 
     output = new long[numOfTrials+1]; 

     //generate random values for each element until the numOfTrials is reached 
     for (int counter = 1; counter <= numOfTrials; counter++) 
     { 
      values[counter] = (int)(Math.random()*(highRange)) + lowRange; 
     } 

     //create output and concatinate numbers 
     for (int counter = 0; counter <= values[0]; counter++) 
     { 
      //add a line break every tenth number 
      //and output the values[] 
      if ((int)(counter/10) == (((double)counter)/10)) 
      { 
       System.out.println(values[counter] + " "); 
      } else { 
       System.out.print(values[counter] + " "); 
      } 

     } 

     System.out.println("Enter 1 to use these values or 0 for new ones"); 

     //regenerate or not? 
     if (input.nextInt() == 1) 
     { 
      goodRandom = true; //continue on the actual test 
     } 

    } //close number-generator loop 

} //close setup method 

} //close Expo class 

Я думаю, что я делаю что-то неправильно с помощью сканера, но я не уверен, что.

+0

Убедитесь, что вы собираете свой файл Java с Java 5 или более поздней версии. –

+0

Какой механизм вы используете для запуска банки? – tbodt

+0

Возникает ли ошибка при компиляции, jar-ifying или запуске программы? – MathSquared

ответ

2

в то время как вы компилируете этот код с помощью jdk 7+, вы действительно используете его с помощью java < = 6. , если вы посмотрите here, вы увидите, что 51.0 - это java 7, и тот факт, что его неподдерживаемый означает, что java на вашем пути к классу старше.

попробуйте запустить java -version в командной строке, чтобы проверить и исправить PATH

+0

Спасибо, это сработало! – user2611929

Смежные вопросы