-12

I have this in the File.java:

 public static void main(String args[]) throws Exception_Exception {
    URL wsdlURL = CallSService.WSDL_LOCATION;
    if (args.length > 0) { 
        File wsdlFile = new File(args[0]);
        try {
            if (wsdlFile.exists()) {
                wsdlURL = wsdlFile.toURI().toURL();
            } else {
                wsdlURL = new URL(args[0]);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();

and I want to transfer this to a JSP file, so I do like that:

List<String> Search(String keyS){

if(keyS!=null){
QName SERVICE_NAME = new QName("http://ts.search.com/", "callSService");





String arg=??????????????;

URL wsdlURL = CallSService.WSDL_LOCATION;

File wsdlFile = new File(arg);
try {
    if (wsdlFile.exists()) {
        wsdlURL = wsdlFile.toURI().toURL();
    } else {
        wsdlURL = new URL(arg);
    }
} catch (MalformedURLException e) {
    e.printStackTrace();
}

I want to replace the args[0] with arg. What does (String args[]) mean? and how can I replace it ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Adams
  • 105
  • 4
  • 10

2 Answers2

8

String args[] is an array of Strings passed in from the command line.

So, if you started your app with java MyApp arg1 arg2

Then args[] would contain => ["arg1", "arg2"]

Java will automatically split up arguments separated by spaces, which is how it knows how many arguments you passed in.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
4

Don't do this in a JSP :(

Don't put your functionality in a main, it's confusing: public static void main is conventionally a program's entry point, not a general purpose method. You may use it as one, but IMO it is misleading.

Instead, create an instance method you can call with the argument you want. It could be a static method, but this builds in some inflexibility making things more difficult to test. Embeddeding the code in a JSP also increases testing difficulty.

You'll need to use ServletContext.getRealPath() to get a file relative to the web app, unless you're providing an absolute path. If the file is "embedded" in the app (on the classpath) you'll want to use one of the resourceAsStream variants.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • i'm usig jsp on a web service so i must do that i need just the values of args[0] to use in my method – Adams Nov 21 '11 at 18:52
  • 4
    Again: don't do this in a `main` function--create a normal function, pass in whatever arguments you need. I don't understand the issue. – Dave Newton Nov 21 '11 at 19:55