3

Only ever used IDEs for developing in Java and wanted to learn how to use JShell but I'm getting the following error for a basic hello world example and anything else I try. Don't understand where the ';' error is coming from.
|javac HelloWorld.java
| Error:
| ';' expected
| javac HelloWorld.java;

code for HelloWorld.java

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

  • Unable to correlate with the command line used and the code shared, how was that executed using jshell? – Naman Nov 06 '18 at 01:03

2 Answers2

1

You don't compile in JShell, you can add the main method and then call it

 public static void main(String[] args) {
     System.out.println("Hello World");
    }
 }

 main(null);

The following examples shows a method being defined and the method run:

jshell> String grade(int testScore) {
 .....
 jshell> grade(88)
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

Here are two different "hello world" programs:

thufir@dur:~/jshell$ 
thufir@dur:~/jshell$ java hello.java
Hello World from Java
thufir@dur:~/jshell$ 
thufir@dur:~/jshell$ cat hello.java
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World from Java");
  }
}


thufir@dur:~/jshell$ 
thufir@dur:~/jshell$ jshell hello.jsh
Hello World
thufir@dur:~/jshell$ 
thufir@dur:~/jshell$ ./hello.jsh 
jshell 11.0.1
Hello World
thufir@dur:~/jshell$ 
thufir@dur:~/jshell$ cat hello.jsh 
//usr/bin/env jshell --show-version "$0" "$@"; exit $?
System.out.println("Hello World")
/exit
thufir@dur:~/jshell$ 

the .jsh or JShell script is executable so can run a few ways. Hope that helps.

Thufir
  • 8,216
  • 28
  • 125
  • 273