Click here to Skip to main content
15,881,794 members
Articles / Web Development / ASP.NET
Tip/Trick

Running a Java applet from an ASP.NET website

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
2 Jan 2013CPOL1 min read 48.3K   8   6
Integration of a Java applet in ASP.NET.

Introduction

This article helps you to understand how to use Java applet in your ASP.NET website.

Background

Many times you need to access local files on client machine from your website. In dot net you can use ActiveX control.

But ActiveX you can run only in IE, and for other browser you need to make some settings which is not safe.

Using the code

Make one jApplet in net beans as given below :

Image 1

add following function code on button click :

Java
public String readFile(String fn) { 
   String thisLine, ret = ""; 
 try { 
   FileInputStream fin =  new FileInputStream(fn); 
   BufferedReader myInput = new BufferedReader 
                     (new InputStreamReader(fin));
   while ((thisLine = myInput.readLine()) != null) {  
     ret += thisLine + "\n";
   } 
 } catch (Exception e) {
   ret = "Cannot load, exception!";
 } 
 return ret;
}

Then build this applet.

Then right click project-> properties-> Web start-> Under signing->Customize->select Self-sign by generated key-> clock OK

This will sign your jar file of your java applet project.

Then make one asp.net website add one folder name it as applet and copy jar files and class files of applet as shown below:

Image 2

Then add below code in .aspx page

XML
<applet code="javaapplication4/TestApplet.class" 
       archive="applet/TestApplet1.jar" width="325" height="325">  

you can also call applet using deploy java script:

XML
<script src="http://www.java.com/js/deployJava.js" type="text/javascript"></script>
<pre><script type="text/javascript">
    var attributes = { code: 'javaapplication4.TestApplet.class',
        archive: 'applet/TestApplet1.jar', 
        width: 325, height: 325 
    };
    var parameters = { fontSize: 16 };
    var version = '1.6';
    deployJava.runApplet(attributes, parameters, version); 
</script>  

then run the website

It will give you following pop up for enabling Java applet

Image 3

Check the check-boxes and click Run

Image 4

Then type any local file name in text box and click on Load

Image 5

It will allow you to read any file on client machine and it will show you content of that file.

You can test this by deploying website on IIS and run it from different machine.

If you want to send some parameter to applet from asp page you can use this link: http://www.roseindia.net/java/example/java/applet/appletParameter.shtml

For more information you can use this link: http://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm

Points of Interest

It will reduce your time and efforts of setting security of browser for ActiveX. It works with all browser which supports Java.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Experience in dot net specifically worked on bio metric devices.

Comments and Discussions

 
QuestionMY japplet shows blank on the browser. Pin
omotoyosi16-Feb-16 21:33
omotoyosi16-Feb-16 21:33 
QuestionSource Code Pin
842144133311-Feb-16 20:53
professional842144133311-Feb-16 20:53 
QuestionCalling .JAR in .NET through aspx page Pin
Manish_0071-Aug-14 0:00
Manish_0071-Aug-14 0:00 
QuestionHelp me! Pin
wingchunpolice6-May-13 16:28
wingchunpolice6-May-13 16:28 
AnswerRe: Help me! Pin
Revathi Vanga4-Sep-13 21:42
Revathi Vanga4-Sep-13 21:42 
GeneralMy vote of 5 Pin
MAU7872-Jan-13 18:23
MAU7872-Jan-13 18:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.