Fernando Anael Cabral Fernando Anael Cabral - 1 year ago 66
Java Question

Can't use the Scanner class

I just started studying JAVA. I'm trying something very simple: Request a argument from the user on the prompt. I made this code:

import java.util.Scanner;

public class Request
public static void main (String[] args)
Scanner s = new Scanner(System.in);
System.out.println("What's your name?");
String name = s.nextLine();
System.out.println("Welcome " + name);

then I compile it, and when I execute it with

Java -cp Request

Instead of a prompt expecting a parameter
It prints all the options of the JAVA command as if I had coded this:

java -help

Why is that happening?

Answer Source

Use java Request, or maybe java -cp . Request ; the -cp option provides the JVM with a classpath, which should be a list of jars or directories containing .class files.

As Klitos mentions, you can set the CLASSPATH environment variable to provide a default classpath. Setting it to include the current directory . is common practice and spare you from having to use -cp . when invoking Java.

In Windows, the variable is %CLASSPATH% and is a ;-separated list of windows pathes.
In Linux (and MacOS AFAIK), the variable is $CLASSPATH and is a :-separated list of unix pathes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download