I am trying to run a java program on Jupyter but am seeing mutliple
UndefinedMagicException
s.
Using the java kernel IJava
version 1.3.0
, and trying to run the following cell:
%%file day.java
import java.util.Calendar;
class day {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DATE);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
System.out.println(year + "/" + month + "/" + day + " " + hour + ":" + minute);
}
}
but does not create day.java
file and raises the error:
io.github.spencerpark.jupyter.kernel.magic.registry.UndefinedMagicException: Undefined cell magic 'file' at io.github.spencerpark.jupyter.kernel.magic.registry.Magics.applyCellMagic(Magics.java:34) at io.github.spencerpark.ijava.runtime.Magics.cellMagic(Magics.java:31) at .(#12:1)
How can I fix the notebook?
Since you are using a java kernel, there is no need to compile and run the program with shell commands. Instead just put the contents of the day#main
method directly in a cell!
And just like the Python kernel, the state is preserved between cell. This means that after evaluating the one above, there are variables year
, month
, etc. that you can use in subsequent cells. Also like in the Python kernel, you can write an expression on the last line which will become the output (Out[*]
) of the cell.
Lastly, for completeness, the approach you've originally taken with the %%file
and %%shell
cell magics is one that is popular when using the Python kernel to run java on the system. i.e. your original notebook would work fine if switching your notebook's kernel to IPython.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With