Java
Java¶
Although we can configure jupyter to run Java interactively, Java is not meant to be run in that way. 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. Compilers turn java code into low level machine code. Compilers also check to make sure our programs are syntatically correct. 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.*
rm: *.class: No such file or directory
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 in jupyter. 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. The following example is best run outside of jupyter (that is, in the terminal directly).
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)
Input In [7], in <cell line: 6>()
4 print ("The temperature in C is:" , cel)
6 if __name__ == "__main__":
----> 7 main()
Input In [7], 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)
File ~/cs-courses/cs134-f22/staff/_env/lib/python3.9/site-packages/ipykernel/kernelbase.py:1174, in Kernel.raw_input(self, prompt)
1167 """Forward raw_input to frontends
1168
1169 Raises
1170 ------
1171 StdinNotImplementedError if active frontend doesn't support stdin.
1172 """
1173 if not self._allow_stdin:
-> 1174 raise StdinNotImplementedError(
1175 "raw_input was called, but this frontend does not support input requests."
1176 )
1177 return self._input_request(
1178 str(prompt),
1179 self._parent_ident["shell"],
1180 self.get_parent("shell"),
1181 password=False,
1182 )
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