Connecting to JMatlink with Applets / JWS

S

saifnobel

Hi,

I recently installed the JMatlink library (to call Matlab from java)
on my computer. Everything works well as long as I use an application
with a main method. For example the following code works well:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;

public class f
{

public static void main(String[] args)
{
JMatLink eng = new JMatLink();
eng.engOpen();
}
}

However, when I use the same code in an applet, things go haywire.
Following is my applet code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;

public class tst extends Applet implements ActionListener
{
Button okButton;

public void init()
{
setLayout(new FlowLayout());
okButton = new Button("Action!");
add(okButton);
okButton.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == okButton)
{
System.out.println("Action");
JMatLink eng = new JMatLink();
eng.engOpen();
repaint();
}
}
}

It compiles well. But when I click on the Action button in the applet,
following are the errors the system throws:

C:\JMatLink>appletviewer dal.html
Action
Exception in thread "AWT-EventQueue-1"
java.security.AccessControlException: acc
ess denied (java.lang.RuntimePermission loadLibrary.JMatlink)
at
java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:323)
at
java.security.AccessController.checkPermission(AccessController.java:
546)
at
java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkLink(SecurityManager.java:
818)
at java.lang.Runtime.loadLibrary0(Runtime.java:817)
at java.lang.System.loadLibrary(System.java:1030)
at tst.actionPerformed(tst.java:25)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:273)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:183)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:173)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:
121)

C:\JMatLink>

I would appreciate if you could help.

Saif

---------

On Dec 2, 7:22 am, (e-mail address removed) wrote:
....
I recently installed the JMatlink library (to call Matlab from java)
on my computer. ....
However, when I use the same code in anapplet, things go haywire.
Following is myappletcode: ....
C:\JMatLink>appletviewer dal.html
Action
Exception in thread "AWT-EventQueue-1"
java.security.AccessControlException: acc
ess denied (java.lang.RuntimePermission loadLibrary.JMatlink)

Applets that load natives need to be 'full-trust'. That means that
not
only does the applet have to be signed, but the user has to 'accept'
the signed code at the prompt.

BTW - if you want to provide a 'link to see the project' to other
people, you are probably better off looking to a JWS launch, than
applets.

This page has some JWS examples, including a combined
applet/application. <http://www.physci.org/jws/#jtest>

--
Andrew T.
PhiySci.org


-------

Thank you so much for your message Andrew. You are absolutely right
that my final aim is to be able to interact with the Matlab engine
remotely over the Internet.

The JWS idea was excellent. I was not aware that such a technology
existed where we could do away with Applets for web applications! I
researched a bit and then installed NetBeans 5.1.1 IDE. This has
capabilities of configuring an application to run with JWS.

I tried again with my earlier code but the problem remains the same.
The following simple swing code (which also calls the Matlab engine)
works well as long as I run it as an application:

import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* @author Saif Ahmad
*/
public class Hello {
public static void main(String[] args) {
JFrame frame = new JFrame("HelloWorldSwing");
final JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
JMatLink eng = new JMatLink();
eng.engOpen();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}

However, when I run the above with JWS, I get the following errors:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javaws.Launcher.executeApplication(Unknown Source)
at com.sun.javaws.Launcher.executeMainClass(Unknown Source)
at com.sun.javaws.Launcher.doLaunchApp(Unknown Source)
at com.sun.javaws.Launcher.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError
at Hello.main(Hello.java:23)
.... 9 more
Caused by: java.security.AccessControlException: access denied
(java.lang.RuntimePermission loadLibrary.JMatLink)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkLink(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at JMatLink.<clinit>(JMatLink.java:147)
.... 10 more

On the other hand, as soon as I comment out the lines:

//JMatLink eng = new JMatLink();
//eng.engOpen();

The swing application runs faultlessly with JWS! Any help would be
greatly appreciated.

Cheers,

Saif

------


The same (or very similar) security sandbox applies to JWS
projects. It is only 'plain old' applications that have no security
manager at all.

There is a lot more I could ask/suggest, but I won't do it ..here
on this group. If you want to pursue this matter, I suggest you
make a post to comp.lang.java.programmer, a group I regularly
read. The only way I notice posts here, is via. 'Google Alerts' -
which take some time to arrive.

--
Andrew T.


-----

Hi Andrew,

I am posting to this group in the hope that you would be able to guide
me to solve my problem.

Best wishes,

Saif
 
A

Andrew Thompson

I am posting to this group in the hope that you would be able to guide
me to solve my problem.

Glad to see you here. OK - to pick up where we left
off. This project will also need to be signed and
all-permissions to run as a web start project.

You are running NetBeans? Good.

NetBeans understands Ant build files - it has Ant 'built in'.
That is easier, because I have some 'premade' examples
of using Ant to create and launch *trusted* applicatons.

The files I am thinking of, are here.
<http://www.physci.org/jws/#fs>
See the <http://www.physci.org/jws/filetest.zip>
download on the right hand side? Download it,
expand it to local disk, then 'import ant project'*
into the IDE.

* That is not 'exact', but I expect there will be a button
or menu item with similar words.

If you run the 'launch' task of the Ant file, it should ..do
everything needed to compile/build/sign and launch the
application onto screen.

I have not had much feedback about how well those
build files work for other people, so if you have any
problems with them - I would like to know.
 
S

saifnobel

Hi Andrew,

I was able to unpack the zipped folder you suggested, load it in
NetBeans IDE, and run the 'launch' task of the Ant file. Everything
worked well with the build. Following are my build messages from
NetBeans:

properties:
compile:
dist:
Building jar: C:\Documents and Settings\Saif Ahmad\Desktop\filetest
\filetest\build\jar\filetest.jar
Deleting: C:\Documents and Settings\Saif Ahmad\Desktop\filetest
\filetest\filetest.keystore
Generating Key for filetest
Generating 1,024 bit DSA key pair and self-signed certificate
(SHA1withDSA) with a validity of 90 days
for: CN=Andrew Thompson, OU=PhySci, O=physci.org, C=CI
[Storing filetest.keystore]
Signing JAR: C:\Documents and Settings\Saif Ahmad\Desktop\filetest
\filetest\build\jar\filetest.jar

Warning:
The signer certificate will expire within six months.
make-launch-file:
launch:
BUILD SUCCESSFUL (total time: 8 seconds)
The application opened too. Also an icon called 'File Test' was
created on the PC Desktop. When I open a file with this application, I
can view it in the text space. However, I don't seem to be saving /
updating info in text files I open using 'File Test'.

Saif
 
A

Andrew Thompson

The application opened too.

Good. You might study that Ant file, try to understand
how it works, what it is doing, and adapt it to *your* project.

Your output underlines that the 'self-signed' certificate
will expire within 6 months. The default length of time
to generate for the 'certificate validity' is less than that,
since self-signed certificates are not recommended for
actual *production* *code*. If you want to change that,
look into the Ant options for validity in..
<http://ant.apache.org/manual/CoreTasks/genkey.html>

Using a self-signed certificate is less optimal than using
a proper certificate verified by a CA. They go to the effort
of verifying who you are, and then they 'vouch' for you by
adding their certificate into the keychain of yours. The
warnings to the end-user are less threatening, if the
code uses a verified certificate.
...Also an icon called 'File Test' was
created on the PC Desktop. When I open a file with this application, I
can view it in the text space. However, I don't seem to be saving /
updating info in text files I open using 'File Test'.

If I recall correctly* - I did not actually go to the point
of *saving* text edits. That is more complicated, and I
did not want to add too much code to the example.

* I would have to review the code, to be sure. But OTOH
your report shows me everything about both that build file
and your environment that I immediately need to know
(Ant works). So that we can proceed.
 
A

Andrew Thompson

Andrew said:
...

If I recall correctly* - I did not actually go to the point
of *saving* text edits. That is more complicated, and I
did not want to add too much code to the example.

Another point is that if the project was not trusted, the
dialogs you see would only appear after you are
*prompted* (by the Java Plug-In) to allow the action -
try the 'sandboxed' example back at the site to see
the differences between the two projects.

The sandboxed one will not prompt at start-up, but *will*
prompt you once the code tries to do anything with the
file services.

The trusted version will prompt you at start-up - if permission
is not granted to 'trust the code' - it never makes it onto
screen, but if trust is granted - there are no further prompts
for the file services.

Since all I wanted to do was demonstrate that the project
was 'trusted' - it seemed redundant to go on to do the
actual file editing.

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
 
S

saifnobel

Thank you so much for valuable advice Andrew. I will try to adapt the
Ant method to my project, and let you know what happens.

Cheers,

Saif
 
S

saifnobel

Thank you so much for valuable advice Andrew. I will try to adapt the
Ant method to my project, and let you know what happens.

Cheers,

Saif

Hi Andrew,

I have tried to read your Ant file as also your code for opening a
file and loading it into a text editor in Java. My understanding is
that the Ant file is essentially an XML document which tells the
system how to build the project with details of what the main class
is, where to place the compiled classes, as also to generate a signed
JWS application and the corresponding JNLP file.

Your java program (FileTest.java) imports some components of the
javax.jnlp API. In your class you have methods of loading the file as
also saving it. I tried to play with this project at a very basic
level. However, I do not seem to understand where I can input the call
to JMatLink engine which will in turn connect to Matlab.

The usual java code to do this here:

JMatLink engine = new JMatLink();
engine.engOpen();

Where engOpen() is a method in class JMatLink.java which will help
open (or communicate with) Matlab. It seems to me that my task is a
little different from loading a file. Details on JMatLink can be found
here:

http://www.held-mueller.de/JMatLink/download.html

JMatLink Version 1.00 has the relevant code. I know how to call the
JMatLink class from an application that has a main method. But I don't
seem to figure out where I could place the above two lines in your
code to be able to do this. Do I need to use some other methods in the
javax.jnlp package?

I would appreciate any help or inputs.

Cheers,

Saif
 
A

Andrew Thompson

...
I have tried to read your Ant file as also your code for opening a
file and loading it into a text editor in Java. ...
..I tried to play with this project at a very basic
level. However, I do not seem to understand where I can input the call
to JMatLink engine which will in turn connect to Matlab.

The usual java code to do this here:

JMatLink engine = new JMatLink();
engine.engOpen();

For the MatLink project, you need to ensure that the Jar
archives (including the MatLink classes) are made
available to the app. at compile-time* and that the
archives (including not only the MatLink classes,
but also any necessary DLLs (or SOs)) are included in
the application's classpath.

* The compile time classpath can be set in a similar way
that I added the JWS classes to the File Test code. The
compiler needs to 'know' where they are, so it can 'understand'
the call to JMatLink.

** The runtime classpath is specified in the JNLP file that is
used. Each Jar of classes should be added to the resources
section of the JNLP as a jar element. The natives can be
also be added (they should be in the 'root' of an archive, and it
will need to be signed) by using a nativelib*** element in the
resources section.

*** A nativelib element is the same form as a jar element.
... Do I need to use some other methods in the
javax.jnlp package?

Many JWS based projects do *not* use, or *need* any of
the JWS specific classes (like the FileOpenService etc.)
so 'I doubt it'. It was simply that this project did.

Try adding the archives as I've specified above, first to get
a successful compilation. If you are having trouble with it
(*even after reference to the Ant documentation*), detail the
error message you are getting from Ant, and we might be
able to help further.

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
 
S

saifnobel

Hi Andrew,

Let me first make sure I got you right. When you say I need to set the
compile time classpath, this is the file you are referring to:

<!-- Build file for the project. -->
<project basedir="." default="launch" name="filetest">

<target name="properties">
<property name="build" value="build" />
<property name="dist" value="dist" />
<property name="src" value="src" />

<!-- Pot luck guess at location of
suitable 'web-start' jar. -->
<property
name="classpath"
value="${java.home}/lib/javaws.jar" />

<!-- Web-start will prompt the user to associate this
file type with the application. Change if it clashes
with an existing file type. -->
<property
name="file.extension"
value="zzz" />
<property
name="file.content-type"
value="text/sleepytime" />
</target>

<target
name="compile"
depends="properties"
description="Compile the project" >
<mkdir dir="${build}/share" />
<javac
debug="on"
destdir="${build}/share"
srcdir="${src}/java"
source="1.2"
classpath="${classpath}" />
<copy todir="${build}/share">
<fileset dir="${src}/java">
<exclude name="**/CVS" />
<exclude name="**/*.java" />
</fileset>
</copy>
</target>

<target
name="dist"
depends="compile"
description="Create project distribution" >
<mkdir dir="${build}/jar" />
<mkdir dir="${build}/jar/lib" />
<jar destfile="${build}/jar/filetest.jar">
<fileset dir="${build}/share">
<include name="**/*.class" />
</fileset>
</jar>
<!-- Generate the keystore -->
<delete file="filetest.keystore" />
<genkey alias="filetest"
storepass="secret"
keystore="filetest.keystore"
verbose="true">
<dname>
<param name="CN" value="Andrew Thompson"/>
<param name="OU" value="PhySci"/>
<param name="O" value="physci.org"/>
<param name="C" value="CI"/>
</dname>
</genkey>
<!-- Sign the jar (required only for trusted launch) -->
<signjar
jar="${build}/jar/filetest.jar"
alias="filetest"
storepass="secret"
keystore="filetest.keystore" />
</target>

<target
name="make-launch-file"
depends="properties"
description="Copies and configures the launch file" >
<copy todir="${build}/jar" >
<fileset dir="${src}/conf" >
<include name="**/*.jnlp" />
</fileset>
</copy>

<replace dir="${build}/jar/">
<include name="**/*.jnlp" />
<replacefilter
token="%file.extension%"
value="${file.extension}" />
<replacefilter
token="%file.content-type%"
value="${file.content-type}" />
</replace>
</target>

<target
name="launch"
depends="dist, make-launch-file"
description="Launch the project using webstart">
<exec executable="javaws"
dir="${build}/jar">
<arg line="-codebase file:. file:./filetest.jnlp" />
</exec>
</target>

<target
name="uninstall"
depends="properties"
description="Uninstall the project from the webstart cache">
<exec executable="javaws">
<arg
line="-uninstall http://www.physci.org/jws/filetest.jnlp"
/>
</exec>
</target>

<target name="clean"
depends="properties"
description="Clean all generated files">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
</project>

In the above, is this the code you are referring to?

<javac
debug="on"
destdir="${build}/share"
srcdir="${src}/java"
source="1.2"
classpath="${classpath}" />
<copy todir="${build}/share">
<fileset dir="${src}/java">
<exclude name="**/CVS" />
<exclude name="**/*.java" />

If I copy the JMatLink.class and JMatLink.dll files to directory "$
{java.home}/lib/, would it do the job since that is the compile
classpath?

Saif
 
L

Lew

Hi Andrew,

Let me first make sure I got you right. When you say I need to set the
compile time classpath, this is the file you are referring to:

<!-- Build file for the project. -->
<project basedir="." default="launch" name="filetest">

<target name="properties">
<property name="build" value="build" />
<property name="dist" value="dist" />
<property name="src" value="src" />

<!-- Pot luck guess at location of
suitable 'web-start' jar. -->
<property
name="classpath"
value="${java.home}/lib/javaws.jar" />
If I copy the JMatLink.class and JMatLink.dll files to directory "$
{java.home}/lib/, would it do the job since that is the compile
classpath?

Bad idea. You should not put code in that directory.

Add another directory to the classpath.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top