Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Converting Java into J/link runnable code

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104429] Converting Java into J/link runnable code
  • From: Garapata <warsaw95826 at mypacks.net>
  • Date: Sat, 31 Oct 2009 01:53:04 -0500 (EST)

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);
    }
  }
______________

I've tried to follow the J/link documentation to translate the above
into something that will work in Mathematica:

Needs["JLink`"];
InstallJava[];

myTimer[numberOfMillisecondsInTheFuture_] :=
 JavaBlock[
  InstallJava[];
  LoadJavaClass["java.sql.Date"];
  LoadJavaClass["java.util.Timer"];
  LoadJavaClass["java.util.TimerTask"];
  LoadJavaClass["java.awt.Toolkit"];

  public class Main
   {
    public static void main[String[] argv] throws Exception
     {

      Date timeToRun = JavaNew["Date", System`currentTimeMillis[] +
numberOfMillisecondsInTheFuture];
      Timer timer = JavaNew["Timer"];
      timer@schedule[JavaNew["TimerTask"]
         {
          public void run[]
           {
            Toolkit`getDefaultToolkit[]@beep[]
            }
          }, timeToRun];
      }
    }
  ]
______________

This gives me lots of errors:

In[15]:= myTimer[300]

During evaluation of In[15]:= Java::excptn: A Java exception occurred:
java.lang.ClassNotFoundException: Date
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:247).

During evaluation of In[15]:= LoadJavaClass::fail: Java failed to load
class Date. >>

During evaluation of In[15]:= Set::write: Tag Times in Date timeToRun
is Protected. >>

During evaluation of In[15]:= Java::excptn: A Java exception occurred:
java.lang.ClassNotFoundException: Timer
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:247).

During evaluation of In[15]:= LoadJavaClass::fail: Java failed to load
class Timer. >>

During evaluation of In[15]:= Set::write: Tag Times in Timer $Failed
is Protected. >>

Out[15]= {{class Exception Main Null public^2 static throws void main[
    argv String[]]}}

__________________

Any ideas to help get this working?

Examples that show Java code against J/link implementation would help
a lot (for the above code or any code).

Thanks...


  • Prev by Date: Re: presentations
  • Next by Date: Re: Re: ToExpression[.., TexForm] does not seem to work on some Latex commands?
  • Previous by thread: How to calculate a union
  • Next by thread: Re: Re: ToExpression[.., TexForm] does not seem to work on some Latex commands?