3

When I try to launch the NetLogo gui from RStudio using the function NLStart(), I get messages about java and the GUI does not open. I am using Win 7 64-bit, NetLogo 5.0.3, and R 2.15.1 and R studio 0.96.304.
Here is the R code...

library(RNetLogo)
nl.path <- "C:\\Program Files (x86)\\NetLogo 5.0.3"
NLStart(nl.path, gui = TRUE, nl.version = 5) 

and returns message

<java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(Unknown Source)
at java.awt.Window.<init>(Unknown Source)
at java.awt.Frame.<init>(Unknown Source)
at java.awt.Frame.<init>(Unknown Source)
at javax.swing.SwingUtilities$SharedOwnerFrame.<init>(Unknown Source)
at javax.swing.SwingUtilities.getSharedOwnerFrame(Unknown Source)
at javax.swing.JOptionPane.getRootFrame(Unknown Source)
at javax.swing.JOptionPane.showOptionDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at javax.swing.JOptionPane.showMessageDialog(Unknown Source)
at nlcon.NLink_v5.<init>(NLink_v5.java:108)>

At first I suspected NetLogo might be running in headless mode, but when I try to open a model using...

model.path <- "\\models\\Sample Models\\Earth Science\\Fire.nlogo"
NLLoadModel(paste(nl.path,model.path,sep=""))

I get

<Error in .jcall(nl.obj, "V", "loadModel", .jnew("java/lang/String", model.path)) : 
RcallMethod: invalid object parameter>

What is going on here? Any solution or clues would be appreciated. Thanks in advance

npell
  • 83
  • 1
  • 8
  • 1- First check that rJava is working `library(rJava)` 2- change the working directory to where you have installed RNetLogo (`setwd(..)`) and try something like `NLStart(getwd())` – agstudy Feb 16 '13 at 23:29
  • I made sure rJava is current and loaded prior to loading RNetLogo and made sure the working directory was set to NetLogo, but still the behavior is unchanged. Are there any other tests I should run to make sure it is working properly? BTW I am running Java 7.0.130, if that is relevant. – npell Feb 17 '13 at 02:34
  • when I type I have `system('java -version')` java version "1.7.0_10", your version is recent. RNetLogo works fine for me.... – agstudy Feb 17 '13 at 04:25

3 Answers3

2

First, please install the latest version of RNetLogo (0.9.6), available at R-Forge, see here http://rnetlogo.r-forge.r-project.org/ (Section "How to get it"). But this will not solve your problem, because we see that it is not a RNetLogo problem but a problem already arising with rJava. Seems that there is something wrong with your Java/configuration (not supporting visual displays --> awt/swing).

You haven't written what kind of Java you have installed (Oracle/Sun JRE, Sun JDK, OpenJDK ...). But I think it is not a Java problem in general (because you have been able to create the Point object which leeds to the conclusion that the awt libraries are installed) but a configuration problem (the JVM started by rJava seems to be running in headless mode).

Because this is really hard to solve by a remote diagnosis, I would try to remove Java (and all related environment variables: JAVA_HOME, corresponding entries in PATH, maybe NOAWT etc.) and rJava, and install clean ones.

But before you do this, I have two further ideas:

  1. You could also try to execute

    Sys.setenv(NOAWT=0)

    before starting the JVM with

    .jinit()

    or before loading the rJava package. But I'm not very optimistic, because calling

    .jcall("java/lang/System", "S", "getProperty", "java.awt.headless")

    should not result in a HeadlessException, as you reported. This looks strange to me. What happens if you do all this stuff (also from the previous answer) without RStudio?

  2. Are you able to install and open JGR (a package for R delivering a Java-based R environment, see http://cran.r-project.org/web/packages/JGR/index.html)? If so, try to start RNetLogo from there.

Good luck!

Jan C. Thiele
  • 239
  • 1
  • 2
  • Thanks, Jan. I have Oracle Java installed, 1.7.0_05. – npell Feb 18 '13 at 20:41
  • 1
    I opened R outside of RStudio and then successfully launched NetLogo using NLStart() from there. Thanks again. – npell Feb 18 '13 at 20:47
  • I am having the same problem, and it doesn't get solved by opening it outside of RStudio, nor by doing any of these things that Jan Suggested. – Kenji Nov 18 '14 at 07:35
  • 1
    @npell have you figured out why it is only possible outside RStudio? – Daniel Mar 20 '20 at 13:11
1

At a first glance it sounds like a Java problem, but I'm not sure. Never saw this error before.

Which Java distribution are you using (OpenJDK, Sun JRE, Sun JDK ...)? Which version of RNetLogo have you installed?

z <- installed.packages()
z["RNetLogo","Version"]

Your error is thrown in line 108, in which RNetLogo tries to open a JOptionPane to report an error happend during NetLogo loading. So, the problem is currently the missing GUI support of your Java. This can also be the underlying problem why NetLogo isn't started but it can also be something different.

My first suggestion is, that you either installed a lightweight version of Java without GUI libraries or it is configured to not using such libraries.

So, start with a first test: Open a MS-DOS prompt, navigate to your NetLogo installation:

cd  "C:\Program Files (x86)\NetLogo 5.0.3"

and try to start NetLogo from there:

java -jar NetLogo.jar

What happened?

The second test, if you pass the first one: Open a new RStudio session and load rJava:

library(rJava)

initialize it:

.jinit()

try to create a simple awt object:

.jnew( "java/awt/Point", 10L, 10L )

and try to open a simple awt window:

f <- .jnew("java/awt/Frame","Hello")
.jcall(f,,"setVisible",TRUE)

This should open a simple window (which is not closeable with the X, because it has no handlers - just close your RStudio). If not, there is something wrong with the awt GUI support of your Java/rJava.

At least a swing JOptionPane test:

component <- .jnull()
component <- .jcast(component, new.class = "java/awt/Component")
message <- .jnew("java/lang/String","This is a JOptionPane test from rJava.")
message <- .jcast(message, new.class = "java/lang/Object")
title <- .jnew("java/lang/String","Test")
type <- .jnew("java/lang/Integer", as.integer(2))
f <- .jnew("javax/swing/JOptionPane")
.jcall(f,,"showMessageDialog", component, message, title, .jsimplify(type))

Bring your RStudio in back and you should see a dialog window. If not, there is something wrong with the swing support of your Java/rJava.

Check, if rJava runs with the Java version you expect:

.jcall("java/lang/System", "S", "getProperty", "java.vm.version")
.jcall("java/lang/System", "S", "getProperty", "java.vm.name")
.jcall("java/lang/System", "S", "getProperty", "java.vm.info")
.jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
.jcall("java/lang/System", "S", "getProperty", "sun.arch.data.model")
.jcall("java/lang/System", "S", "getProperty", "java.vm.info")

What reports this:

.jcall("java/lang/System", "S", "getProperty", "java.awt.headless")

and this:

Sys.getenv("NOAWT")

?

Jan C. Thiele
  • 239
  • 1
  • 2
  • > z <- installed.packages() > z["RNetLogo","Version"] [1] "0.9.5" > library(rJava) > .jinit() [1] 0 > .jnew( "java/awt/Point", 10L, 10L ) [1] "Java-Object{java.awt.Point[x=10,y=10]}" > f <- .jnew("java/awt/Frame","Hello") Error in .jnew("java/awt/Frame", "Hello") : java.awt.HeadlessException – npell Feb 18 '13 at 04:54
  • > .jcall("java/lang/System", "S", "getProperty", "java.vm.version") Error in .jcall("java/lang/System", "S", "getProperty", "java.vm.version") : java.awt.HeadlessException – npell Feb 18 '13 at 04:58
  • > .jcall("java/lang/System", "S", "getProperty", "java.vm.name") [1] "Java HotSpot(TM) 64-Bit Server VM" – npell Feb 18 '13 at 04:58
  • > .jcall("java/lang/System", "S", "getProperty", "java.vm.info") [1] "mixed mode" – npell Feb 18 '13 at 04:59
  • > .jcall("java/lang/System", "S", "getProperty", "java.runtime.version") [1] "1.7.0_05-b05" – npell Feb 18 '13 at 05:00
  • > .jcall("java/lang/System", "S", "getProperty", "sun.arch.data.model") [1] "64" – npell Feb 18 '13 at 05:00
  • > .jcall("java/lang/System", "S", "getProperty", "java.vm.info") [1] "mixed mode" – npell Feb 18 '13 at 05:00
  • > .jcall("java/lang/System", "S", "getProperty", "java.awt.headless") [1] "true" – npell Feb 18 '13 at 05:00
  • > Sys.getenv("NOAWT") [1] "" – npell Feb 18 '13 at 05:01
  • Also, I was able to launch NetLogo using the command prompt as you describe. – npell Feb 18 '13 at 05:04
  • What is going on with the "HeadlessException".. It turns up in another poster here: http://stackoverflow.com/questions/13297028/getting-java-awt-headlessexception-on-running-jasperviewer-viewreportjasperprin – npell Feb 18 '13 at 05:11
1

I found that having all my software (NetLogo, R and Java) as either 64 bit (which works for me) or 32 bit is fine. Opening this in 2021 might be slightly different. I find that running the code directly into the R GUI instead of from RStudio also helps the first time. I have a youtube video (https://www.youtube.com/watch?v=zWMdaTEPTOc) of how to open NetLogo from R including troubleshooting options. My functional code can also be found here. https://raw.githubusercontent.com/tonysdatamodels/netlogo.in.r/main/open%20netlogo%20in%20r