Program can't count arguments

L

lonelyplanet999

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.
 
G

Gordon Beaton

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
 
R

Roedy Green

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.

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. :)
 
L

lonelyplanet999

Roedy Green said:
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"
 
L

lonelyplanet999

Gordon Beaton said:
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.
 
M

Matt Humphrey

lonelyplanet999 said:
Gordon Beaton <[email protected]> wrote in message
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 (e-mail address removed) http://www.iviz.com/
 
L

lonelyplanet999

Roedy Green said:
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 :)
 
D

David Postill

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

<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-----
 
R

Roedy Green

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.
 
W

Wojtek

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%
 
L

lonelyplanet999

David Postill said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

<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 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.
 
L

lonelyplanet999

Wojtek said:
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%

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top