Program can't count arguments  
Author Message
lonelyplanet999





PostPosted: 2003-10-28 13:08:00 Top

java-programmer, Program can't count arguments Hi,

I have written a .java file with below source code.

public class getargs {
public static void main (String [] args) {
System.out.println(args.length);
}
}

Compilation succeeded. But when I ran the program with below
parameters:

java getargs abc
or
java getargs "abc"

the program returned 0 in both cases. If I don't intertpret wrongly,
args.length should contain the count of program arguments. Why would I
get the result as I ran this program ?

I run java SDK 1.4.1.
 
Gordon Beaton





PostPosted: 2003-10-28 14:09:00 Top

java-programmer >> Program can't count arguments On 27 Oct 2003 21:07:58 -0800, lonelyplanet999 wrote:
> I have written a .java file with below source code.
>
> public class getargs {
> public static void main (String [] args) {
> System.out.println(args.length);
> }
> }
>
> Compilation succeeded. But when I ran the program with below
> parameters:
>
> java getargs abc
> or
> java getargs "abc"
>
> the program returned 0 in both cases. If I don't intertpret wrongly,
> args.length should contain the count of program arguments. Why would
> I get the result as I ran this program ?

Do you mean that 0 was printed or returned? In both of your examples,
the program should print 1 and return 0.

If it isn't printing 1, then maybe you aren't running the program you
think you are. Do you have more than one version of "getargs.class"?

Alternatively, what is java? Is it really the original java launcher
in the JDK (or JRE), or is it a script that itself calls the java
launcher? If the latter, maybe it isn't passing on the command line
arguments correctly to "real" java. Try specifying the full path to
the real java launcher when you run the program.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
lonelyplanet999





PostPosted: 2003-10-28 20:45:00 Top

java-programmer >> Program can't count arguments Roedy Green <email***@***.com> wrote in message news:<email***@***.com>...
> On 27 Oct 2003 21:07:58 -0800, email***@***.com
> (lonelyplanet999) wrote or quoted :
>
> >public class GetArgs {
> > public static void main( String[] args ) {
> > System.out.println( args.length );
> > }
> >}
> java GetArgs abc
>
> I ran this with Java 1.4.2_02 and it worked fine giving the answer 1.

I changed the naming convention as yours, I ran it with java 1.4.1_01,
but my program still printed '0' to my computer screen no matter I
typed

java GetArgs abc or java GetArgs "abc"

>
> The only change I made was to follow Sun naming conventions, and
> called it GetArgs. Perhaps the Java Gods don't like you ignoring
> naming conventions. :-)
 
 
lonelyplanet999





PostPosted: 2003-10-28 20:50:00 Top

java-programmer >> Program can't count arguments Gordon Beaton <email***@***.com> wrote in message news:<3f9e15fe$email***@***.com>...
> On 27 Oct 2003 21:07:58 -0800, lonelyplanet999 wrote:
> > I have written a .java file with below source code.
> >
> > public class getargs {
> > public static void main (String [] args) {
> > System.out.println(args.length);
> > }
> > }
> >
> > Compilation succeeded. But when I ran the program with below
> > parameters:
> >
> > java getargs abc
> > or
> > java getargs "abc"
> >
> > the program returned 0 in both cases. If I don't intertpret wrongly,
> > args.length should contain the count of program arguments. Why would
> > I get the result as I ran this program ?
>
> Do you mean that 0 was printed or returned? In both of your examples,
> the program should print 1 and return 0.

I mean 0 was printed to computer screen.

>
> If it isn't printing 1, then maybe you aren't running the program you
> think you are. Do you have more than one version of "getargs.class"?

I have only one version of getargs.class. The same happens even if
after I change getargs.* to GetArgs.*

>
> Alternatively, what is java? Is it really the original java launcher
> in the JDK (or JRE), or is it a script that itself calls the java

It's JSDK 1.4.1_01. I downloaded it from sun's web site.

> launcher? If the latter, maybe it isn't passing on the command line
> arguments correctly to "real" java. Try specifying the full path to
> the real java launcher when you run the program.

I stored the .java & .class files under directory c:\javapgm.
I created a .bat file inside c:\javapgm named jav.bat, it's content is

c:\j2sdk1.4.1_01\bin\java %1.

I typed jav GetArgs to run the class.

>
> /gordon
 
 
Matt Humphrey





PostPosted: 2003-10-28 20:57:00 Top

java-programmer >> Program can't count arguments
"lonelyplanet999" <email***@***.com> wrote in message
news:email***@***.com...
> Gordon Beaton <email***@***.com> wrote in message
news:<3f9e15fe$email***@***.com>...
> > On 27 Oct 2003 21:07:58 -0800, lonelyplanet999 wrote:
> > > I have written a .java file with below source code.
> > >
> > > public class getargs {
> > > public static void main (String [] args) {
> > > System.out.println(args.length);
> > > }
> > > }
> > >
> > > Compilation succeeded. But when I ran the program with below
> > > parameters:
> > >
> > > java getargs abc
> > > or
> > > java getargs "abc"
> > >
> > > the program returned 0 in both cases. If I don't intertpret wrongly,
> > > args.length should contain the count of program arguments. Why would
> > > I get the result as I ran this program ?
> >
> > Do you mean that 0 was printed or returned? In both of your examples,
> > the program should print 1 and return 0.
>
> I mean 0 was printed to computer screen.
>
> >
> > If it isn't printing 1, then maybe you aren't running the program you
> > think you are. Do you have more than one version of "getargs.class"?
>
> I have only one version of getargs.class. The same happens even if
> after I change getargs.* to GetArgs.*
>
> >
> > Alternatively, what is java? Is it really the original java launcher
> > in the JDK (or JRE), or is it a script that itself calls the java
>
> It's JSDK 1.4.1_01. I downloaded it from sun's web site.
>
> > launcher? If the latter, maybe it isn't passing on the command line
> > arguments correctly to "real" java. Try specifying the full path to
> > the real java launcher when you run the program.
>
> I stored the .java & .class files under directory c:\javapgm.
> I created a .bat file inside c:\javapgm named jav.bat, it's content is
>
> c:\j2sdk1.4.1_01\bin\java %1.
>
> I typed jav GetArgs to run the class.

And that explains the problem because it's only passing the GetArgs
parameter (%1). You need to add the other parameters from the command line.

Cheers,
Matt Humphrey email***@***.com http://www.iviz.com/


 
 
lonelyplanet999





PostPosted: 2003-10-29 1:50:00 Top

java-programmer >> Program can't count arguments Roedy Green <email***@***.com> wrote in message news:<email***@***.com>...
> On 27 Oct 2003 21:07:58 -0800, email***@***.com
> (lonelyplanet999) wrote or quoted :
>
> >public class GetArgs {
> > public static void main( String[] args ) {
> > System.out.println( args.length );
> > }
> >}
> java GetArgs abc
>
> I ran this with Java 1.4.2_02 and it worked fine giving the answer 1.

Yes, you're right. I know what happens now. What I stupid mistake :[
I start the .class file via a batch file called jax.bat

It's content is

c:\j2sdk1.4.1_01\bin\java %1.

So if I called jax GetArgs abc 123

the parameters abc & 123 will be trimmed by the batch file.

My new question is, if I kept on using this batch file, how can I
modify it to cater for variable number of arguments passed to the java
program ?

Tks :)

>
> The only change I made was to follow Sun naming conventions, and
> called it GetArgs. Perhaps the Java Gods don't like you ignoring
> naming conventions. :-)
 
 
David Postill





PostPosted: 2003-10-29 2:47:00 Top

java-programmer >> Program can't count arguments -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In article <email***@***.com>, on 28 Oct 2003 09:49:40 -0800, email***@***.com
(lonelyplanet999) wrote:

<snip />

| c:\j2sdk1.4.1_01\bin\java %1.
|
| So if I called jax GetArgs abc 123
|
| the parameters abc & 123 will be trimmed by the batch file.
|
| My new question is, if I kept on using this batch file, how can I
| modify it to cater for variable number of arguments passed to the java
| program ?

*Please* read the help files that come with your operating system!

Use %* instead of %1

<davidp />

- --
David Postill

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0.3 - not licensed for commercial use: www.pgp.com
Comment: Get key from pgpkeys.mit.edu:11370

iQA9AwUBP564enxp7q1nhFwUEQIAegCYlqob2n1jkLKw/CPrM1UUQbg7+wCXTGLJ
tVxQAkllqAoxx4ycaYvYiQ==
=jinR
-----END PGP SIGNATURE-----

 
 
Wojtek





PostPosted: 2003-10-29 12:48:00 Top

java-programmer >> Program can't count arguments On 28 Oct 2003 09:49:40 -0800, email***@***.com
(lonelyplanet999) wrote:

>My new question is, if I kept on using this batch file, how can I
>modify it to cater for variable number of arguments passed to the java
>program ?

Use the following for any number of arguments. This works for more
than just 9
-----------------------------
@echo off

set allargs=

:LOOP
set allargs=%allargs% %1
shift
if not "%1"=="" goto LOOP

java yourprog %allargs%
-----------------------------

------------------------
Wojtek Bok
Solution Developer
 
 
lonelyplanet999





PostPosted: 2003-11-2 12:24:00 Top

java-programmer >> Program can't count arguments David Postill <email***@***.com> wrote in message news:<email***@***.com>...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> In article <email***@***.com>, on 28 Oct 2003 09:49:40 -0800, email***@***.com
> (lonelyplanet999) wrote:
>
> <snip />
>
> | c:\j2sdk1.4.1_01\bin\java %1.
> |
> | So if I called jax GetArgs abc 123
> |
> | the parameters abc & 123 will be trimmed by the batch file.
> |
> | My new question is, if I kept on using this batch file, how can I
> | modify it to cater for variable number of arguments passed to the java
> | program ?
>
> *Please* read the help files that come with your operating system!
>
> Use %* instead of %1

I replaced batch file (jav.bat) content from javac %1 to javac %*

The result is it couldn't read further arguments. Each time I typed
jav -source 1.4 Argu.java, DOS extended my batch file just as javac *.
So compilation definition failed.

public class Argu {
public static void main (String[] args) {
for (int i=0; i<2; i++)
{
assert(i>=0);
System.out.println(args[i]);
}
}
}

I adopted Roedy Green's suggestion and use javac %1 %2 %3 ... %7.
Compilation succeed.

Then I changed batch file (jax.bat) content from java %1 to java %*.
As I run jax Argu 1 2 3, the batch file expanded it as java * and of
course exception triggered in main method.

Remark: My JVM ran under windows 98. I opened a DOS window to do
compilation and execution of compiled .java files.

>
> <davidp />
>
> - --
> David Postill
>
> -----BEGIN PGP SIGNATURE-----
> Version: PGP 8.0.3 - not licensed for commercial use: www.pgp.com
> Comment: Get key from pgpkeys.mit.edu:11370
>
> iQA9AwUBP564enxp7q1nhFwUEQIAegCYlqob2n1jkLKw/CPrM1UUQbg7+wCXTGLJ
> tVxQAkllqAoxx4ycaYvYiQ==
> =jinR
> -----END PGP SIGNATURE-----
 
 
lonelyplanet999





PostPosted: 2003-11-2 12:26:00 Top

java-programmer >> Program can't count arguments Roedy Green <email***@***.com> wrote in message news:<email***@***.com>...
> On 28 Oct 2003 09:49:40 -0800, email***@***.com
> (lonelyplanet999) wrote or quoted :
>
> >My new question is, if I kept on using this batch file, how can I
> >modify it to cater for variable number of arguments passed to the java
> >program ?
>
> just provide %1 %2 %3 .... and only use some of them.


Tks! It works for both javac & java!
 
 
lonelyplanet999





PostPosted: 2003-11-2 12:27:00 Top

java-programmer >> Program can't count arguments Wojtek <email***@***.com> wrote in message news:<email***@***.com>...
> On 28 Oct 2003 09:49:40 -0800, email***@***.com
> (lonelyplanet999) wrote:
>
> >My new question is, if I kept on using this batch file, how can I
> >modify it to cater for variable number of arguments passed to the java
> >program ?
>
> Use the following for any number of arguments. This works for more
> than just 9
> -----------------------------
> @echo off
>
> set allargs=
>
> :LOOP
> set allargs=%allargs% %1
> shift
> if not "%1"=="" goto LOOP
>
> java yourprog %allargs%
> -----------------------------
>
> ------------------------
> Wojtek Bok
> Solution Developer

Can this concept be applied to javac ?

e.g. I want to use a batch file to run below:

javac -source 1.4 <myprogram.java>