gpt4 book ai didi

Java newInstance 传递 args[1]...args[args.length]

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:39 33 4
gpt4 key购买 nike

(旁白:我是一名 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:

我做错了什么,导致移位后无法成功传递参数?

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()

关于Java newInstance 传递 args[1]...args[args.length],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22284339/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com