How to add jar into applet? - java

I have JApplet which use some jars. I have added these jars to lib folder of my applet, have set these to classpath and have created html:
<HTML>
<HEAD>
<TITLE>Applet</TITLE>
</HEAD>
<BODY>
<H1>Applet</H1>
<object classid="java:com.csat.CSATApplet.class"
type="application/x-java-applet"
archive="file:///C:\\Documents and Settings\\alburash\\Desktop\\CSAT_client_2.jar"
height="300" width="450" >
<PARAM NAME=ARCHIVE VALUE="dom4j-1.6.1.jar,poi-3.10-FINAL-20140208.jar,poi-excelant-3.10-FINAL-20140208.jar,poi-ooxml-3.10-FINAL-20140208.jar,poi-ooxml-schemas-3.10-FINAL-20140208.jar,poi-scratchpad-3.10-FINAL-20140208.jar,stax-api-1.0.1.jar,xmlbeans-2.3.0.jar,axis.jar,commons-discovery-0.2.jar,javax.wsdl_1.6.2.v201012040545.jar,jaxrpc.jar,org.apache.commons.logging_1.1.1.v201101211721.jar,org.apache.xmlrpc_3.0.0.v20100427-1100.jar,saaj.jar">
</object>
</BODY>
</HTML>
But when I start this html I see error "java.lang.NoClassDefFoundError:javax/xml/rpc/Service". I understand that the applet can't find this class, but why does it happen? When I start the applet via Eclipse it works correct. All needed libraries are into "lib" folder into CSAT_client_2.jar , classpath is correct, CSATApplet.class have been found(it was my previous problem).
It's unsigned applet.
For Stephen C: I have tried to change html and the applet can't find CSATApplet.class and don't start. When I use my first html, the applet finds CSATApplet.class without problem and starts, but after that I see my first error. My main problem is that I can start applet, but the applet doesn't find jar files with needed libraries.
<HTML>
<HEAD>
<TITLE>Applet</TITLE>
</HEAD>
<BODY>
<H1>Applet</H1>
<object classid="java:com.csat.CSATApplet.class"
type="application/x-java-applet"
codebase="file:///C:/Documents%20and%20Settings/alburash/Desktop/CSAT_client_2.jar"
height="300" width="450" >
</object>
</BODY>
</HTML>
Also, when I start the applet in Eclipse I see warning, but the applet starts and works correctly:
"Apr 30, 2014 2:09:06 PM org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled."

According to the HTML 4.0.1 spec, section 13.3, the "archive" attribute is a space separated list of URLs. The implication is that it is the list of URLs for the JARS that comprise the applet's classpath.
Consider putting all of the JARs into one directory and using the "codebase" attribute so that you can use relative URLs in the "archive" URL list.
Your "file:" URL is malformed. A well-formed Windows "file:" URL looks like this:
file://laptop/My%20Documents/FileSchemeURIs.doc
or
file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc
Note that:
the path separator in a hierarchical URL is "/", and not "\" or "\",
any space characters need to be %-escaped.
Finally, I could not find any justification in the spec or in the Oracle pages on applets that would support using a <param name='archive' ... > element to specify the applet classpath.
Reference:
File URLs in Windows

You can use applet tag:
<applet codebase="."
code="com.csat.CSATApplet.class"
archive="CSAT_client_2.jar,dom4j-1.6.1.jar,poi-3.10-FINAL-20140208.jar,poi-excelant-3.10-FINAL-20140208.jar,poi-ooxml-3.10-FINAL-20140208.jar,poi-ooxml-schemas-3.10-FINAL-20140208.jar,poi-scratchpad-3.10-FINAL-20140208.jar,stax-api-1.0.1.jar,xmlbeans-2.3.0.jar, axis.jar,commons-discovery-0.2.jar,javax.wsdl_1.6.2.v201012040545.jar,jaxrpc.jar,org.apache.commons.logging_1.1.1.v201101211721.jar,org.apache.xmlrpc_3.0.0.v20100427-1100.jar,saaj.jar"
height="300"
width="450">
</applet>
By this way, both the HTML page and JAR files must have in same directory

Related

Deploying applet with 3rd party library

I am trying to use gson library with my applet but JRE(8u25) does not see the library and gives me this error:
MyAppSigned.jar is my signed applet
gson-2.3.1.jar is the library i am trying to use
Test WebPage Content (Test.html):
<html>
<body>
<applet code='test.XApplet' width=400 height=400>
<param name='ARCHIVE' value='MyAppSigned.jar,gson-2.3.1.jar'>
<param name='codebase' value='http://example.com/Commons'>
</applet>
</body>
</html>
Here is the content of the manifest file inside MyAppSigned.jar:
Manifest-Version: 1.0
Trusted-Library: true
Class-Path: gson-2.3.1.jar
Permissions: all-permissions
Created-By: 1.6.0_26 (Sun Microsystems Inc.)
Codebase: *
Name: test/XApplet.class
SHA1-Digest: qLHEgL7Or0Ja7Jn7iRZt2lJ/928=
Here is the content of commons directory on iis:
I tried with/without codebase attribute in my test webpage. it does not changed the error.
But if i copy gson-2.3.1.jar into the directory C:\Program Files\Java\jre1.8.0_25\lib\ext it Works without any error
what am i doing wrong?
First of all copy the jar in JRE_HOME/lib/extis not a good solution since then you've to copy this jar in all the client machines which will use your applet.
I think that the problem with your code is in your <applet> definition, you're using <param> to define archive, however in documentation archive and others are defined as attributes of <applet> tag, take a look at documentation. So try defining the <applet> as follows:
<html>
<body>
<applet code="test.XApplet" width="400" height="400"
archive="MyAppSigned.jar,gson-2.3.1.jar" codebase="http://example.com/Commons">
</applet>
</body>
</html>
Note that it's better to use <object> since <applet> tag will not be supported in HTML5, also think about use deployJava.js which can make more easy to deploy an applet in a different browsers.
Hope this helps,

Class not found exception Applet.jar in html file

I am trying to run an .jar file with a class in it that extends Applet in an html file. The .jar is called DocScrabble.jar, html is DocScrabble.html, and .class file is ScrabbleSolver.class. ScrabbleSolver.class also references a file called EnglishWords.txt, and I included that in the default package in which I placed ScrabbleSolver.class when I exported the file to a .jar. DocScrabble.jar and DocScrabble.html are located in the same directory. The applet works fine in eclipse, so I am assuming that my html file is the problem. THere is the html code.
<!DOCTYPE html>
<html>
<head>
<title>Doc Scrabble></title>
</head>
<body>
<APPLET CODE="ScrabbleSolver.class"
ARCIHVE="DocScrabble.jar"
WIDTH="400"
HEIGHT="200"
</Applet>
</body>
</html>
When i try to run the html, it gives me an error that says ClassNotFoundException ScrabbleSolver.class. Could someone please tell me what's wrong? I'm relatively new to programming.
It is highly advisable to check HTML using a validation service or DTD.
I typically rely on the W3C HTML validation service to check HTML.
Note that the applet element was last valid in HTML 3.2. It was deprecated in HTML 4.01. Without declaring any version, that mark-up would be presumed to be HTML 5.
The best way to deploy an applet is to use the Deployment Toolkit Script. On the other hand mistakes in spelling the attribute names in the script would not be picked up by an HTML validation service, since it concentrates on the HTML, rather than JavaScript embedded in the HTML.
For that reason it is a good idea to see the applet work when loaded using 'pure HTML' first.
Is your ScrabbleSolver class in a package? if so then it should be packagename.ScrabbleSolver.class
See Also: http://download.java.net/jdk8/docs/technotes/guides/jweb/applet/using_tags.html

Where I save the applet in tomcat?

I call an applet through a JSP...where I have to save it? and what the .class and the .java file? in which root in tomcat so when I call it from the JSP to be appeared?
here is the applet call from the jsp
%><%# page language="java"%>
<html>
<body>
<jsp:plugin code="g7appletDialog.class" codebase="" type="applet" width="300" height="200">
<jsp:fallback>Unable to load applet</jsp:fallback>
</jsp:plugin>
<applet code=""g7appletDialog.class"" width="300" heigjt="200"></applet>
</body>
</html><%
The path to your servlet must be relative to your jsp. If you have a jsp in web-inf/admin/pages/index.jsp, then you can place the .class file in directory web-inf/admin/pages/ and just have code="g7appletDialog.class".
In case that you have a central repository for applets, lets say web-inf/admin/applets then you should change the code to code="../applets/g7appletDialog.class".
The ../ gets you one dir back. To go two dirs back use ../../ and so on...

How to reach css and image files from the html page loaded by javafx.scene.web.WebEngine#loadContent?

I have a String HTML content which is loaded into webEngine by loadContent() method. I have also some css and image files used in this page. Although I put these file into the same package of java class, the loaded page cannot find them. Looked for API docs and web, but could not find any appropiate similar solutions. How I load these files?
You can place your string html content in a file in the same package as the Java class and use the engine.load(String url) method instead:
engine.load(getClass().getResource("mypage.html").toExternalForm());
When you do this, all relative links in the html page will resolve to resources (e.g. css and image files) in your Java package.
Beware that if you are loading a resource that is located in a jar file, that the jar: protocol does not understand relative links with parent specifiers. E.g., <img src="../images/image.png"/> will not work, but <img src="/images/image.png"/> or <img src="images/image.png"/> will as long (as you put the image in the appropriate location in the jar file). The file: protocol does not have such restrictions and .. relative links will work fine when the resources are loaded using it.
If the html string is dynamically generated by your java code rather than static, then Sergey's solution is probably best.
You need to set your local paths in html string for loadContent next way:
view.getEngine().loadContent(
"<img src='" + getClass().getResource("1.jpg") + "' />");
I just found out that using the <base> tag in the HTML also does the trick:
<html>
<head>
<title>The slash at the end of the href is important!</title>
<base href="file:///absolute/path/to/your/docroot/" />
</head>
<body>
<img src="image.png"/>
</body>
</html>
If you load the above code via engine.loadContent(String) then image.png will be loaded from /absolute/path/to/your/docroot/image.png.
This method is easier if you need to load multiple resources since you only have to specify the absolute path at a single place.
This has been tested with WebView of Java 8u25.
Try this
<link href="file:css\default.css" rel="stylesheet" type="text/css" />

Java applet won't load

I am building a small applet in java. It works fine (really!) when I run it in eclipse as an applet. Problems start when I export it as a jar and than try to load it through an html page.
Here's the html code:
<body>
<applet archive="myJar.jar" width=650 height=850>
</applet>
</body>
Now when I run other jar files in this way they work fine, for example if you view the source page of this niffty demo
<body>
<applet code="org.niffty.Niffty.class" archive="niffty.jar" width=650 height=850>
<param name="file" value="prelude.nif">
</applet>
</body>
Thank you!
You're missing the code attribute in your applet tag. Without it, the browser doesn't know which class in your JAR file is the applet it should display.
You've specified the archive, but no code attribute so the applet runner doesn't know which class to use as the entry point. You have to either specify code or specify the object attribute to give a serialized applet instance - but code is much more likely to be appropriate for you.
See the documentation for the applet tag for more information.

Categories