File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Getting error while displaying tif image in applet through browser. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Getting error while displaying tif image in applet through browser." Watch "Getting error while displaying tif image in applet through browser." New topic
Author

Getting error while displaying tif image in applet through browser.

dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
Hi,

I have an applet which displays tif image using JAI api.
When I try to run applet as standalone program it work fine and display image but when I call this applet through jsp it doesnt work.
It gives error like

java.lang.NoClassDefFoundError: com/sun/media/jai/codec/SeekableStream
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


I have installed JAI api in JDK directory and I am using Netbeans IDE.

Applet tag in JSP file is as below
<applet code="com.bsic.pimg.MyApplet" archive="ImageViewer.jar">

ImageViewer.jar file contains class file for MyApplet.java.

So can you please tell me what is wrong ?

Thanks in advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
Welcome to JavaRanch.

Does ImageViewer.jar contain the JAI classes as well? If not, then you need to add the JAI jar file to the archive attribute.


Ping & DNS - my free Android networking tools app
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
Hi,

I have included jai_core.jar and jai_coded.jar in archive attribute.
When I run the jsp it was run without error.
In bottom of the browser it shows "Applet com.bsic.pimg.MyApplet started".
But it does not show tif image. Whole page comes blank.

so what can be the problem ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
Check the Java Console to see if there are any error messages.
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
when I right click on browser it does not give any opition for java console.
Even in my Netbeans ide there is no any error.
i am using mozilla firefox.
Also browser gets hanged after runnig application.

so how can i check java console ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
The Java Conole can be accessed through the Java Plugin control panel. If you're on Windows, then there should be a small coffee cup icon in the tray on the lower right hand side of the screen that you can use as well.
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
it shows the error

java.security.AccessControlException: access denied (java.io.FilePermission D:\BsicRw\Tomcat5.2\webapps\PImgViewer\web\PolImages\002201001_111036_014.tif read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:264)
at java.security.AccessController.checkPermission(AccessController.java:427)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at java.io.FileInputStream.<init>(FileInputStream.java:100)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at com.bsic.pimg.MyApplet.init(MyApplet.java:52)
at sun.applet.AppletPanel.run(AppletPanel.java:380)
at java.lang.Thread.run(Thread.java:595)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
So it looks as if the applet isn't signed correctly. When starting the applet, do you get the little popup window that asks you to accept the certificate?
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
no i have not got any such popup.
how can i signed the applet ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
See HowCanAnAppletReadFilesOnTheLocalFileSystem
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
it seems long process for signing applet and also require purchase of certificate.
I just want to show image by reading it from local directory.

Is there any other way to make it possible ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
No, signing an applet isn't particularly hard, and you can create the certificate yourself using the tools shipped with the JDK. There's no need to purchase anything.

You need to use "keytool" to create a key/certificate, and then "jarsigner" to use the certificate to sign the jar. Shouldn't take more than an hour to figure it out and then do it.
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
Hi
I have follow the step as given in link SigningApplet

I have completed upto step 6.

Path for my html is D:\NetBeansProjects\ImageViewerWeb\web
From where I have to run html file TestImg.html

content of html is as
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Hello World!</h2>
<applet code="com.bsic.pimg.MyApplet" archive="SImageViewer.jar,jai_codec.jar,jai_core.jar" width="400" height="600">
</applet>
</body>
</html>


Path for the following files is same as TestImg.html

CompanyCer.cer
compstore
dksstore
SImageViewer
Write.jp

content of the Write.jp file is as below

keystore "D:/NetBeansProjects/ImageViewerWeb/web/dksstore";

grant SignedBy "company" {
permission java.io.FilePermission
"D:/", "read";
};



I have executed following commands as applet developer

(1) keytool -genkey -alias signFiles -keystore compstore -keypass pass123 -dname "cn=dks" -storepass pass456

(2) jarsigner -keystore compstore -storepass pass456 -keypass pass123 -signedjar SImageViewer.jar ImageViewer.jar signFiles

(3) keytool -export -keystore compstore -storepass pass456 -alias signFiles -file CompanyCer.cer

After that as a client have exectued following command

(1) keytool -import -alias company -file CompanyCer.cer -keystore dksstore -storepass pass789

When I run following command

appletviewer -J-Djava.security.policy=Write.jp TestImg.html.html

getting the same error

java.security.AccessControlException: access denied (java.io.FilePermission D:\UnderTheSun.gif read)


So can you please guide me is there anything i have missed out in signing applet ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
You don't need to alter any policy files. If the applet is signed, then that is redundant (it's an alternative method to grant permissions).

There's also no need for the "keytool -export" and "keytool -import" steps.

Note that all involved jar files need to be signed - that includes the JAI jar files.
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
I have signed JAI jar files and tried to run html but it still gives the access denied error.
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
Hi Dear,

At last I have done it. Now I can see TIFF file in browser.

Thanks for giving your valuable time in response.

Thank you very much.
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
On my machine it runs perfectly.
When i try to run link from another machine it give popup with run button.
When I click run button image does not get displayed.

Should I have to do import and export step for that ?

how can I acces it from another machine ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
When i try to run link from another machine it give popup with run button. When I click run button image does not get displayed.

Any error messages in the Java Console?

Should I have to do import and export step for that ?

No. Those steps are not needed for signing an applet.
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
When I see another machine's console it gives FileNotFoundException for image path.

I have given image path in applet hardcoded of my local machine. So is applet trying to find that path on client's machine ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
So is applet trying to find that path on client's machine ?

Of course. An applet can access files only on the local machine where it's running, not on some other machine. Should this particular image be used wherever the applets is run? If so, put it on the web server along with the other applet files, and access it as shown in the LoadAGif example.
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
I have two applicatin in Netbeans.
One is ImageViewer which contains my applet which shows image.
Second is ImageViewerWeb which contains index.jsp to display image in browser
and packages jar file generated by ImageViewer.

I have tried putting image in two places
(1) In ImageViewer I have put image where applet is.
(2) In ImageViewerWeb I have put image where applet jar file resides.

for both cases it does not work when i access in another machine.

So where to place image ?

Is there any way to access server path through applet ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
Forget about NetBeans. You need to put all those files on a proper web server for another machine to be able to access them. What's the location of all the files involved? What are the error messages you're getting?
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
location of all jar files and jsp file is ImageViewerWeb/Web/

Getting exception as

java.io.FileNotFoundException: \ImageViewerWeb\002201001_111036_014.tif (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.bsic.pimg.MyApplet.init(MyApplet.java:136)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

my code for getting image path is

URL url = getCodeBase();
path = url.getPath()+"002201001_111036_014.tif";
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
URL url = getCodeBase();
path = url.getPath()+"002201001_111036_014.tif";

That's not how you load images in applets, particularly not from the remote server. You can get an InputStream from which to read the image using something like:

URL url = new URL(getCodeBase() + "/002201001_111036_014.tif");
InputStream is = url.openStream();
dhiren sangani
Greenhorn

Joined: May 22, 2007
Posts: 18
Hi,

Now I can view TIFF image successfully on client site also.
Below is the my modified code.

MediaTracker tr = new MediaTracker(this);

URL url = new URL("http://serveripadddress:port/applicationname/tiffilename");

ReadableByteChannel rBytChnl = Channels.newChannel( url.openStream() );
ByteBuffer buffer = ByteBuffer.allocate(4096*1024);
rBytChnl.read(buffer);

byte[] data = buffer.array();
SeekableStream stream = new ByteArraySeekableStream(data);
String[] names = ImageCodec.getDecoderNames(stream);
ImageDecoder dec = ImageCodec.createImageDecoder(names[0], stream, null);
RenderedImage im = dec.decodeAsRenderedImage();
Image image = PlanarImage.wrapRenderedImage(im).getAsBufferedImage();

img = image.getScaledInstance(500, -1, Image.SCALE_SMOOTH);
System.out.println("image: " + path + "\n" + image);
tr.addImage(img,0);


Thanks a lot for your quick response.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting error while displaying tif image in applet through browser.