Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Converting Java into J/link runnable code

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104461] Re: Converting Java into J/link runnable code
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Sun, 1 Nov 2009 03:59:45 -0500 (EST)
  • References: <hcgmt3$cui$1@smc.vnet.net>

Garapata wrote:
> J/link seems like a good way to go to set up timers so, I found this
> short Java example that sets up a recurring beep.  If I can get it to
> work, I can do a lot of other things with the ideas:
> 
> import java.sql.Date;
> import java.util.Timer;
> import java.util.TimerTask;
> import java.awt.Toolkit;
> 
> public class Main {
>   public static void main (String[] argv) throws Exception {
>     Date timeToRun =
>      new Date (System.currentTimeMillis() +
> numberOfMillisecondsInTheFuture);
>     Timer timer = new Timer();
> 
>     timer.schedule (new TimerTask() {
>         public void run() {
>           Toolkit.getDefaultToolkit.beep();
>           }
>         }, timeToRun);
>     }
>   }
> ______________

First, the Java code as written is partly schematic because the variable 
numberOfMillisecondsInTheFuture needs to be replaced by an actual value. 
Also, the Timer should be set up as a Daemon thread by passing the 
argument true. If you make these changes, you end up with some runnable 
Java code:

import java.sql.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Toolkit;

public class Main {
   public static void main (String[] argv) throws Exception {
     Date timeToRun =
      new Date (System.currentTimeMillis() +
1117);
     Timer timer = new Timer(true);

     timer.schedule (new TimerTask() {
         public void run() {
           Toolkit.getDefaultToolkit().beep();
           }
         }, timeToRun);
     }
   }

To test this code, save it in a file Main.java, and type

javac Main.java
java Main

OK - so now you have some Java code that works on its own, so now you 
can try hooking it up to Mathematica. First change the Class name to 
something more sensible - e.g. MyTimerClass, and provide a method which 
is not called Main, and which accepts the actual time delay you require:

import java.sql.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Toolkit;

public class MyTimerClass {
   public static void myTimer (int numberOfMillisecondsInTheFuture) 
throws Exception {
     Date timeToRun =
      new Date (System.currentTimeMillis() +
numberOfMillisecondsInTheFuture);
     Timer timer = new Timer(true);

     timer.schedule (new TimerTask() {
         public void run() {
           Toolkit.getDefaultToolkit().beep();
           }
         }, timeToRun);
         System.out.println("????????");
     }
   }

This should be stored in a file called MyTimerClass.java (the names of 
source files are important in java) and compiled:

javac MyTimerClass.java

This will make a file MyTimerClass.class

Here is the Mathematica code required to call this static method:

In[1]:= Needs["JLink`"]

In[3]:= AddToClassPath["c:\\myjava"];

In[4]:= LoadJavaClass["MyTimerClass"];

In[8]:= MyTimerClass`myTimer[2000]

Replace the path c:\\myjava" with the directory containing your .class 
file, and away you go!

Note that you can't just splice the Java source into your Mathematica 
program as you have done! Also, you do not need to load the classes that 
are used internally by your Java code.

Alternatively, it would be possible to avoid compiling any Java code, by 
breaking up the steps inside myTimer into individual J/Link calls, but 
this is somewhat less general, and probably more error prone.

David Bailey
http://www.dbaileyconsultancy.co.uk


  • Prev by Date: Re: How to calculate a union
  • Next by Date: Re: Re: ToExpression[..,TexForm] does not seem
  • Previous by thread: Re: DateListBarChart / Ticks in BarChart
  • Next by thread: Re: Converting Java into J/link runnable code