Java

Although we can configure jupyter to run Java interactively, Java is not meant to be run interactive. Doing so in jupyter requires additional packages to be installed. Instead, we are going to use jupyter to create files and run commands as though we were running them on our Terminal.

In this first example, we are creating a file called Hello.java.

%%file Hello.java

public class Hello {

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

}
Overwriting Hello.java

Java programs must be compiled before we can run them. Compiling a java program creates a class file. Before compiling, let’s remove all previously compiled class files and look at what’s left.

%%bash 

rm *.class
ls Hello.*
Hello.java

Now let’s compile our program. After compiling, if we use ls again to see the contents of our directory, a class file now exists.

%%bash

javac Hello.java
ls Hello.*
Hello.class
Hello.java

Now let’s run our program using the java command.

%%bash

java Hello
Hello, world!

As an aside, note that we could use a similar technique with Python. It’s usually more convenient to run Python interactively, but it would work in the same way.

%%file hello.py

def main():
    print("Hello, World!")
    
if __name__ == "__main__":
    main()
Overwriting hello.py
%%bash

python3 hello.py
Hello, World!

Note that this technique (of creating files and running commands as though we are on the Terminal) doesn’t work well when input is required, unfortunately. See the following example.

def main ():
  fahr = input ("Enter the temperature in F: " )
  cel = (float(fahr) - 32) * 5.0/9.0
  print ("The temperature in C is:" , cel)

if __name__ == "__main__":
    main()
---------------------------------------------------------------------------
StdinNotImplementedError                  Traceback (most recent call last)
/var/folders/md/kwd9nc_d2ns0hw9wsvdrnt2c0000gn/T/ipykernel_85923/2965695217.py in <module>
      5 
      6 if __name__ == "__main__":
----> 7     main()

/var/folders/md/kwd9nc_d2ns0hw9wsvdrnt2c0000gn/T/ipykernel_85923/2965695217.py in main()
      1 def main ():
----> 2   fahr = input ("Enter the temperature in F: " )
      3   cel = (float(fahr) - 32) * 5.0/9.0
      4   print ("The temperature in C is:" , cel)
      5 

/usr/local/lib/python3.9/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
   1001         """
   1002         if not self._allow_stdin:
-> 1003             raise StdinNotImplementedError(
   1004                 "raw_input was called, but this frontend does not support input requests."
   1005             )

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
%%file TempConv.java

import java.util.Scanner;

public class TempConv {
    public static void main (String args[]) {
        Double fahr;
        Double cel;
        Scanner in;

        in = new Scanner (System.in);
        System.out.print("Enter the temperature in F: ");
        fahr = in.nextDouble ();

        cel = ( fahr - 32) * 5.0/9.0;
        System.out.println("The temperature in C is: " +cel);
    }
}
Overwriting TempConv.java
%%bash

javac TempConv.java
ls TempConv.*
TempConv.class  TempConv.java