This site requires JavaScript, please enable it in your browser!
Greenfoot back
Freddy05
Freddy05 wrote ...

2021/7/8

Java Exception for my code in Greenfoot

Freddy05 Freddy05

2021/7/8

#
Hi there, i recently recoded my game "treasure hunter". i added some functunality to scale the game based on the desktops screen dimensions. Locally everithign runs fine, But if i upload it to greenfoot it gives me a java exception "Exception instanciating world class: java.lang-RuntimeException - (javaScript) ReferenceError: jl_NoClassDEFFoundError__init_0 is not defined". can someone reproduce this error and maybe help me troubleshoot it. I need compatibility for the web in this game, because i want to host it on my website via embed. (my game with open sourec code so oyu can have a look: https://www.greenfoot.org/scenarios/27705) my appologies if you dont understand somethign ( im german) and didnt comment much. My start world is Welcomescreen
RcCookie RcCookie

2021/7/8

#
The error is occurring because the web version does not actually run native java. Instead, your code get‘s converted to JavaScript so that browsers can execute it natively without plugins. The problem is that the conversion is - well, quite bad. It is lacking a lot of libraries that standard java does offer, like java.awt, Java.io and nio or javax, making it very restrictive when doing anything other than moving an actor. Furthermore, there are actually quite a few bugs in the converter. The error message is a really good example: It shows that a class you used does not exist, but the funny thing is the the error that would normally be thrown, a „NoClassDefFoundError“, does not even exist itself. To get more information on the exception, right-click somewhere in the browser onto the background and select „Untersuchen“ or „Inspitieren“, however it is called, and go to the „Konsole“ tab. Some good news though: JavaScript is an interpreted language, meaning that each line of code is only evaluated once it should actually be executed. That means that you can use the code you have written, but make sure it’s not executed online. I actually made a utility class just for that purpose, checking weather the scenario it running online or offline
Freddy05 Freddy05

2021/7/8

#
Thanks for this answer, This isn’t really a solution, but a good workaround, Just a quick question, how would I implement this check? Normally I would create a new object, but you wrote, that this class is not to be created. could you give me an example on how another class could get the online status? edit: already figured it out!
RcCookie RcCookie

2021/7/8

#
You can look into the code example provided in the scenario. Also, feel free to ask further questions right here.
RcCookie RcCookie

2021/7/8

#
One more thing I wanted to add: you can in fact use the libraries that are not included, by creating them yourself (copy the source code). This bad not work with a huge library (unless you find it on GitHub or so) and thus ain’t help you this time, but if you are just missing a single class it may be worth considering.
Freddy05 Freddy05

2021/7/10

#
Thanks for the Tip, dofinetly thinking about this when the project is fully finished. so it can be played in the Browser :)
Freddy05 Freddy05

2021/7/12

#
Looking back on what you said, i just tried to add java.awt.Toolkit.java into my code, but i dont really know how to. Do i need to make it a abstract class like in the source or how do i use it? i used this as source. can you give me a brief overview on how to do this?
nccb nccb

2021/7/12

#
AWT is a Java graphics toolkit for the desktop. Greenfoot's online translator doesn't support AWT because it would have to recreate the whole framework inside the browser. So unfortunately you won't be able to use AWT classes. The issue at hand seems to be that you want to get the resolution of the desktop; I don't think there's any way to do this that would work through the Java->Javascript compilation. The best I can think to do is to have multiple versions of your scenario at different scales, and decide using some external Javascript which version to show based on the user's resolution. Or have an options menu in-game where they can choose their scale.
Freddy05 Freddy05

2021/7/12

#
Thanks for the info, considering to do this :)
RcCookie RcCookie

2021/7/12

#
I don’t know - java has ways to invoke JavaScript commands. But as far as I know that also doesn’t work online. But if someone can get that to work that would open a whole new set of options.
You need to login to post a reply.