mardi 23 juin 2015

how to run a java application using java command [duplicate]

This question already has an answer here:

I'm reading book called "Learning Java" by Patrick Niemeyer & Daniel Leuck and I got to this section about Accessing Methods, it's under the section titled Reflection which explains how to use the Java Reflection API. In The Accessing Methods section, there is some example code that is supposed to access a method.

//file: Invoke.java
import java.lang.reflect.*;

class Invoke {
  public static void main( String [] args ) {
    try {
      Class c = Class.forName( args[0] );
      Method m = c.getMethod( args[1] );
      Object ret =  m.invoke( null );
      System.out.println(
          "Invoked static method: " + args[1]
          + " of class: " + args[0]
          + " with no args\nResults: " + ret );
    } catch ( ClassNotFoundException e ) {
        System.out.println( e );
      // Class.forName(  ) can't find the class
    } catch ( NoSuchMethodException e2 ) {
        System.out.println( e2 );
      // that method doesn't exist
    } catch ( IllegalAccessException e3 ) {
        System.out.println( e3 );
      // we don't have permission to invoke that method
    } catch ( InvocationTargetException e ) {
        System.out.println( e );
      // an exception ocurred while invoking that method 
      System.out.println(
          "Method threw an: " + e.getTargetException(  ) );
    }  

  }
}

He goes on to say "We can run invoke to fetch the value of the system clock:"

      % java Invoke java.lang.System currentTimeMillis

      Invoked static method: currentTimeMillis of class:

      java.lang.System with no args

      Results: 861129235818

The first line is what we type into the java command I think. I can't open the java command, in the beginning of the book theres a section called Running A Java Application that tells us to open the java command but when I searched for it in Start, I found it but when I tried to open it, it just opened and then closed immediately. What do I do.

java version "1.8.0_45", 64-Bit Windows 7 and I have all my environment variables set correctly.





Aucun commentaire:

Enregistrer un commentaire