簡體   English   中英

Java newInstance傳遞args [1]…args [args.length]

[英]Java newInstance passing args[1]…args[args.length]

(此外:我是一個Perl程序員,正如您所知道的,這是我的第一個簡單的Java程序。簡單的用語將不勝感激。)

我有以下啟動器已編碼工作:

import java.lang.reflect.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;

/*
    The following class was cobbled together by a Perl guy ...
*/
class LaunchOnLocal {

    public static void main(String[] args) {
        System.err.println("LaunchOnLocal.main ...");
        WebDriver driver=new FirefoxDriver();
        try {
            // The following works but passes arg[0] to the constructor ..
            Object o=createObject(Class.forName(args[0]).getConstructor(new Class[] {WebDriver.class, String[].class}),new Object[] {driver,args});
            /* Fails ... Here I'm trying NOT to pass arg[0]
            String[] passingArgs=new String[args.length-1];
            System.arraycopy(args,1,passingArgs,0,passingArgs.length);
            Object[] passingArgsArray={passingArgs};
            Object o=createObject(Class.forName(args[0]).getConstructor(new Class[] {WebDriver.class, String[].class}),new Object[] {driver,passingArgsArray});
            */
             }
        catch (ClassNotFoundException e) {
            e.printStackTrace(System.err);
             }
        catch (NoSuchMethodException e) {
            e.printStackTrace(System.err);
             }
        finally {
            driver.close();
            driver.quit();
            System.err.println("... LaunchOnLocal.main");
             };
         }; // main:

    public static Object createObject(Constructor constructor,Object[] arguments) {
        System.err.println("LaunchOnLocal.createObject ...");
        System.err.println("Constructor: "+constructor.toString());
        Object object=null;
        try {
            object=constructor.newInstance(arguments);
            System.err.println("Object: "+object.toString());
            //return object;
             }
        catch (InstantiationException e) {
            e.printStackTrace(System.err);
             }
        catch (IllegalAccessException e) {
            e.printStackTrace(System.err);
             }
        catch (IllegalArgumentException e) {
            e.printStackTrace(System.err);
             }
        catch (InvocationTargetException e) {
            e.getCause.printStackTrace(System.err);
             }
        finally {
            System.err.println("... LaunchOnLocal.createObject");
            return object;
             }
         }; // createPbkect:

     }; // LaunchOnLocal:
/*
*/

按照編碼,啟動器將其所有參數“ args”傳遞給正在啟動的應用程序。 我需要在傳遞args之前刪除args [0]。 我已經嘗試了注釋掉的代碼,但是失敗了

java LaunchOnLocal Test one two
LaunchOnLocal.main ...
LaunchOnLocal.createObject ...
Constructor: public Test(org.openqa.selenium.WebDriver,java.lang.String[]) throws java.lang.InterruptedException
java.lang.IllegalArgumentException: argument type mismatch
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at LaunchOnLocal.createObject(LaunchOnLocal.java:41)
        at LaunchOnLocal.main(LaunchOnLocal.java:20)
... LaunchOnLocal.createObject
... LaunchOnLocal.main

為了完整起見,我包括正在啟動的應用程序:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;

public class Test {

    public Test (WebDriver driver, String[] args) throws InterruptedException {
        System.out.println("Test.Test ...");

        for (String arg: args) {
            System.out.println(arg);
             };

        driver.navigate().to("http://www.sojicity.com/");
        Thread.sleep(10000);
        // Just so we can crash!
        int i=1;
        //i=0; // uncomment this line to cause an error
        i=i/i;
        System.out.println("... Test.Test.");
         }; // Test:

     }; // Test:

我在轉換后無法成功傳遞args,這是我做錯了什么?

fge建議的更正工作!

Object o=createObject(Class.forName(args[0]).getConstructor(new Class[] {WebDriver.class, String[].class}),new Object[] {driver,args});

Object o=createObject(Class.forName(args[0]).getConstructor(new Class[] {WebDriver.class, String[].class}),new Object[] {driver,Arrays.copyOfRange(args, 1, args.length)});

嘗試使用:

Arrays.copyOfRange(args, 1, args.length)

代替。 這要簡單得多,並且無論如何都會在內部使用System.arrayCopy()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM