Running compiled programs in command prompt

A

Albert

Hi, I should know how to do what it says in the subject of this post,
but I don't. I've almost finished Sams Teach Yourself Java in 24 Hours
3rd Edition and now I need to pass some arguments to the program via
command prompt but the book only covers 1.4.

java program [arguments] when i cd it to the directory containing the
class

doesn't work.
 
K

Knute Johnson

Albert said:
Hi, I should know how to do what it says in the subject of this post,
but I don't. I've almost finished Sams Teach Yourself Java in 24 Hours
3rd Edition and now I need to pass some arguments to the program via
command prompt but the book only covers 1.4.

java program [arguments] when i cd it to the directory containing the
class

doesn't work.

public class test7 {
public static void main(String[] args) {
for (String arg : args)
System.out.println(arg);
}
}

C:\Documents and Settings\Knute Johnson>java test7 one two three
one
two
three

It doesn't matter which version of the JDK that you have, command line
arguments have not changed.
 
A

Albert

Albert said:
Hi, I should know how to do what it says in the subject of this post,
but I don't. I've almost finished Sams Teach Yourself Java in 24 Hours
3rd Edition and now I need to pass some arguments to the program via
command prompt but the book only covers 1.4.
java program [arguments] when i cd it to the directory containing the
class
doesn't work.

public class test7 {
public static void main(String[] args) {
for (String arg : args)
System.out.println(arg);
}

}

C:\Documents and Settings\Knute Johnson>java test7 one two three
one
two
three

It doesn't matter which version of the JDK that you have, command line
arguments have not changed.

I can't get this to work:

class DivideNumbers {
public static void main(String[] arguments) {
if (arguments.length == 2) {
int result = 0;
try {
result = Integer.parseInt(arguments[0]) /
Integer.parseInt(arguments[1]);
System.out.println(arguments[0] + " divided by " +
arguments[1] + " equals " + result);
}
catch (NumberFormatException e) {
System.out.println("Both numbers must be numbers");
}
catch (ArithmeticException e) {
System.out.println("You cannot divide by zero.");
}
}
}
}

java dividenumbers 2 2
 
A

Arne Vajhøj

Albert said:
I can't get this to work:

class DivideNumbers {
public static void main(String[] arguments) {
if (arguments.length == 2) {
int result = 0;
try {
result = Integer.parseInt(arguments[0]) /
Integer.parseInt(arguments[1]);
System.out.println(arguments[0] + " divided by " +
arguments[1] + " equals " + result);
}
catch (NumberFormatException e) {
System.out.println("Both numbers must be numbers");
}
catch (ArithmeticException e) {
System.out.println("You cannot divide by zero.");
}
}
}
}

java dividenumbers 2 2

It would have been a bit easier to troubleshoot if you
had posted the error !

But a guess:

java -cp . dividenumbers 2 2

Arne
 
P

Patricia Shanahan

Albert said:
Albert said:
Hi, I should know how to do what it says in the subject of this post,
but I don't. I've almost finished Sams Teach Yourself Java in 24 Hours
3rd Edition and now I need to pass some arguments to the program via
command prompt but the book only covers 1.4.
java program [arguments] when i cd it to the directory containing the
class
doesn't work.
public class test7 {
public static void main(String[] args) {
for (String arg : args)
System.out.println(arg);
}

}

C:\Documents and Settings\Knute Johnson>java test7 one two three
one
two
three

It doesn't matter which version of the JDK that you have, command line
arguments have not changed.

I can't get this to work:

class DivideNumbers { ....

java dividenumbers 2 2

Have you tried "java DivideNumbers 2 2"?

Patricia
 
A

Albert

Albert said:
Albert wrote:
Hi, I should know how to do what it says in the subject of this post,
but I don't. I've almost finished Sams Teach Yourself Java in 24 Hours
3rd Edition and now I need to pass some arguments to the program via
command prompt but the book only covers 1.4.
java program [arguments] when i cd it to the directory containing the
class
doesn't work.
public class test7 {
public static void main(String[] args) {
for (String arg : args)
System.out.println(arg);
}
}
C:\Documents and Settings\Knute Johnson>java test7 one two three
one
two
three
It doesn't matter which version of the JDK that you have, command line
arguments have not changed.
I can't get this to work:
class DivideNumbers { ...

java dividenumbers 2 2

Have you tried "java DivideNumbers 2 2"?

Patricia

Thanks a lot. Problem solved
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top