IJava icon indicating copy to clipboard operation
IJava copied to clipboard

Error: io.github.spencerpark.jupyter.kernel.magic.registry.UndefinedMagicException: Undefined cell magic 'file' -how to compile/run java program on jupyter

Open zainul1114 opened this issue 4 years ago • 3 comments

HI, Plese help me how to compile/run my java program on jupyter

my IJava version: 1.3.0 and my jupyter related versions are jupyter 1.0.0 jupyter-client 6.1.2 jupyter-console 6.1.0 jupyter-core 4.6.3 jupyter-server 0.1.1 jupyterhub 1.1.0 jupyterlab 2.1.0

I am running java program with jupyterhub ,

%%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); } }

i am not able to create .java file and i am getting below 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)

can you please help me to reslove this issue. Else share me any link/guide to reslove this.

zainul1114 avatar May 08 '20 09:05 zainul1114

same here The problem is also present with magic strings in the provided binder links, so it may be a problem with the program itself, the author has not updated he project since last year, maybe we can fix it. https://github.com/SpencerPark/IJava/blob/master/src/main/java/io/github/spencerpark/ijava/runtime/Magics.java

Also checkout this repo ->> frankfliu/IJava<<- this guy kept updating it.

https://mybinder.org/v2/gh/SpencerPark/ijava-binder/master?urlpath=lab https://mybinder.org/v2/gh/SpencerPark/ijava-binder/master

image image

3xau1o avatar Jun 02 '20 18:06 3xau1o

Hi @zainul1114 and @saulpalv, this is expected, there is no file magic. This question looks familiar to one I saw on stackoverflow but for completeness I'll answer here as well.

The file magic shouldn't be necessary for this use case (there are other cases where it is useful, but not for running hava code). Just get rid of the %%file ... line and run the code directly

- %%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);
- }
- }

i.e. all you should need in a cell is:

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);

SpencerPark avatar Jun 22 '20 06:06 SpencerPark

@saulpalv I'll admit a broken example is not great in the demo but it fails because the folder /usr/share/java doesn't exist. The magic should actually be working provided it is given sensible path.

An exception that says Undefined cell magic '<name>' (or line magic) is raised when a magic function is invoked but not registered. Similar to calling a function that doesn't exist. Magics are just special syntax for calling functions. There are some open issues about supporting more of the magic functions that IPython supports because, as shown here, new users come to this kernel expecting them to exist. file is one such case.

On the other hand Exception occurred while running line magic '<name'> is thrown when calling a magic function that does exist but failed for whatever reason. Here it is Exception resolving jar glob

SpencerPark avatar Jun 22 '20 06:06 SpencerPark