Embed Java Applet on GitHub Page

2019-08-05 11:14发布

问题:

I am working on a Java project that I am hosting on GitHub. I wanted to use GitHub Pages to have a page hosted on GitHub with the applet embedded. Now, I'm trying to embed an applet into the page, with the applet files hosted on GitHub on the gh-pages branch. I exported the Java applet as "exploded", so all of the class files show up in their correct hierarchy. In `index.html, I'm using this code:

<section id="main_content">
            <script>
                var attributes = {codebase: 'cubesorter/me/nrubin29/cubesorter/',
                    code: 'Viewer.class',
                    archive: 'cubesorter.jar',
                    width: '800',
                    height: '600'};
                var parameters = {java_arguments: '-Xmx256m'}; // customize per your needs
                var version = '1.5'; // JDK version
                deployJava.runApplet(attributes, parameters, version);
            </script>
        </section>

However, I get a ClassNotFoundException for Viewer.class. Do I have everything set up correctly?

回答1:

I can't leave comments yet, but I'm wondering if the ClassNotFoundException is referring to one of your classes that you wrote or a class from a dependency. Are you depending on external .jar files? They might be on your development environment but not in deployment. Perhaps you need something like Maven to make sure everything (including all the classes) is being deployed.



回答2:

You need to move viewer.class into the cubesorter/tree/gh-pages directory. Simple fix. Not much more to it.

 <!-- try the applet tag instead -->
 <applet src="cubesorter.jar" code="https://github.com/nrubin29/cubesorter/blob/gh-pages/cubesorter/me/nrubin29/cubesorter/Viewer.class?raw=true">Java not supported</applet>

 <!-- <section id="main_content">
        <script>
            var attributes = {codebase: 'cubesorter/me/nrubin29/cubesorter/',
                //How is this even parsed?
                code: 'https:\/\/github.com\/nrubin29\/cubesorte\r/blob/gh-pages/\cubesorter\/me\/nrubin29\/cubesorter\/Viewer.class?raw=true',
                archive: 'cubesorter.jar',
                width: '800',
                height: '600'};
            var parameters = {java_arguments: '-Xmx256m'}; // customize per your needs
            var version = '1.5'; // JDK version
            deployJava.runApplet(attributes, parameters, version);
        </script>
 </section> -->

In the code above, I hotlinked viewer.class for you