User1
User1

Reputation: 41163

Prevent Java from parsing the command line parameters

Would like to make anapplication in Java that will not automatically parse parameters used on the command-line. Currently, java requires public static void main(string[]) as the entry point signature. I would like just a single string that I parse myself. Can this be done at all?

Here's an example: java MyProgram Hello World

I would want it to give me Hello World without requiring quotes around that string. I would even settle for java giving me the entire java MyProgram Hello World. I'm thinking this is something beyond Java and has more to do with the shell.

Upvotes: 1

Views: 1026

Answers (7)

zellio
zellio

Reputation: 32484

This is more of a hack than anything and is tackling the problem from the other side of things but there is a variable in bash called IFS or internal field separator which tells the shell what to consider a delimiter. If one were to wrap up a read with a change in IFS you could gain the same basic effect you are going for albeit with the tedium of needing to put in the data manually every time.

Just a suggestion, bash below.

#!/bin/bash

IFS="
"

read TMP

java $1 \"$TMP\"

unset IFS

Upvotes: 1

Chris Nava
Chris Nava

Reputation: 6802

No. It's not java that's parsing the command line but the shell. In order to prevent it you must quote the input.

java MyProgram "Hello World"

Upvotes: 0

Brent Writes Code
Brent Writes Code

Reputation: 19603

You're correct about the shell part. Assuming you're on Unix, your Unix shell will split your input string on whitespace and then hand you back each piece as an element in the array that is input to "main". The only way to get everything in as a single string is to quote it like this (which you said you didn't want):

java MyProgram 'Hello World'

If you don't want to do that, the easiest thing to do is just recombine them into a string:

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    for(String s : args) {
        sb.append(s);
        sb.append(" "); //some length of whitespace
    }

    String yourString = sb.toString().trim();
}

The real problem here is that if you try to avoid quoting and you recombine the input strings yourself, you're going to lose the amount of whitespace between each word. If you recombine, all of the following command lines:

java MyProgram Hello World
java MyProgram Hello    World
java MyProgram Hello                           World

will still just show up as "Hello World" inside your program.

Upvotes: 6

OscarRyz
OscarRyz

Reputation: 199215

Just join the strings:

class MyProgram {
    public static void main(String [] args ) {
        String commandLine = join( args );
        System.out.println(commandLine);
        // go on 
     }
     private static String join( String [] args ) {
         StringBuilder builder = new StringBuilder();
         for( String arg : args ) {
             builder.append( arg );
             builder.append( ' ' );
         } 
         builder.delete( builder.length()-1, builder.length());
         return builder.toString();
      }
}

Output:

output http://img443.imageshack.us/img443/2682/capturadepantalla201003v.png

Upvotes: 1

Taylor Leese
Taylor Leese

Reputation: 52300

StringBuilder b = new StringBuilder();
for (string s : commandLineArgs) {
    b.append(s);
    b.append(" ");
}

// do what you want with b

Upvotes: 0

Christoph
Christoph

Reputation: 4401

Quick and Dirty fix - just concatenate all the parameters that are passed in.

Upvotes: 0

middus
middus

Reputation: 9121

Java always requires this signature. You can build a single string from the parameters in your main-method, though.

See this for examples on how to do this.

Upvotes: 1

Related Questions