Posts Tagged ‘Java’

How to start a Java application with web start

Friday, December 14th, 2007

These are some notes I made while trying to get a Java OpenAL (JOAL) application start from a web page. The tutorial applies to Java 1.4.2, but it probably works similarly in newer versions.

There are some things you need to master:

  1. Generate a manifest that indicates the main class
  2. Package everything in jar file(s)
  3. Generate your keys using java keytool
  4. Sign the jar file(s)
  5. Make the JNLP XML file
  6. Put everything on a web server

(more…)

How to set up a Java JDK

Wednesday, December 12th, 2007

This refers to doing it on Windows XP - Vista is probably very similar.

For starters…

Java is a programming language, but it’s also something called a virtual machine. The way it works is you write code - source code - in text files, typically ending in .java. Then you compile them, using a java compiler. There’s a command line java compiler called javac, included in the JDK. When you’ve compiled the programs, they don’t become Windows executable programs - “.exe files” - but compiled java class files, files called .class. These you can run, but to do that you need a java runtime environment, a “JRE”. Most likely you already have a bunch of Java runtimes on your computer.

The Java Development Kit - JDK - contains what you need to compile java source code files to class files, and comes with a java runtime to run them. The JDK does not come with an editor, but you can those for free elsewhere. One really good is eclipse (www.eclipse.org). Another good tool for building Java files (and other stuff) is something called Ant (ant.apache.org).

(more…)

How to run java MIDP applications on a Palm handheld

Wednesday, December 12th, 2007

The problem

I have a Java MIDP application, made with WTK, maybe using EclipseME or mobile processing. I want to run it on my Palm.

After digging into ways of doing this, I wound up running an application called jartoprc but it kept giving me “Error: An invalid PRC was created (0 bytes in size).”.

The solution

Prerequisites

I’m assuming you already know how to set up Java JDK and about building MIDP applications - maybe how to get started using mobile processing.

Step by step

  1. Get the websphere micro edition - one way is it might be bundled with your Palm, another is buying it from Palm [where was it? Have to look], and another is joing PalmSource Developer ACCESS network, after which you can download it for free. You should do this anyway to apply for a palm developer id.
  2. Install websphere - unpacking it, e.g. in c:\SDK\websphere
  3. Install the websphere java runtime on your palm. (J9JavaVMMidp20.prc and j9pref.prc)
  4. Add C:\SDK\websphere\Tools\bin to your PATH (Control Panel > System, Advanced tab, click Environment Variables, edit the System variable called Path)
  5. Here’s the trick: the files in the C:\SDK\websphere\Tools\lib folder - charconv.zip and the folder jclFoundation10 containing classes.zip, locale.zip and maps.zip - should all be copied to your JAVA_HOME/lib folder. Copy the single zip file and the folder. It will not work (at least not for me) to add these to your classpath in other ways.
  6. When you have the jad and jar file, go to the directory and build the prc using a command like jartoprc -jar:MyApp.jar -id:MYID
  7. Double-click on the .prc and then hotsync to transfer it to your palm.
  8. The MIDP application should end up on your Palm and you can launch it just like a regular Palm app, just tap it.

References

http://pluggedin.palm.com/regac/pluggedin/JavaFAQ.jsp

How to use min() and max() - and a DIY unit test

Thursday, November 8th, 2007

So, there I was, coding a little thing in Processing. I was just testing out this thing where the user could drag control points around in a window, and I just needed to make sure she didn’t drag them out of bounds. What I want to achieve is this: I have a value that comes from somewhere - an x coordinate from user interaction, say - and I want to limit that value so it’s not smaller than the left edge and not bigger than the right one. I’ve done this lots of times before but I always have to think about which way is which. Well, here it is, once and for all.

The tricky part is that in this case you want to use min to check for the maximum value, and max to check for the minimum value. Very backwards.

int limit(int minValue, int value, int maxValue){
return max(minValue, min(value, maxValue));
}

The inner min(value, maxValue) takes the value of whichever is smaller - the value or the max value - so that’s checking that the value is not too big. The outer max checks the lower limit.

If you want to have a look at the actual code I wrote (complete with comments and error checking) there’s an attached file with the original Processing (in this case, pure Java) code.

Since I was doing this in Processing (and for some reason not from Eclipse but from the rather crappy Processing IDE) there’s not really any unit testing framework around - so I did that as well. The idea behind unit testing is that if you know how your method is supposed to work, it’s a good idea to verify that it does indeed do what you think it does. This seems like a pretty obvious idea, but people were coding along happily for years (and many still are) before thinking of actually testing the parts before testing the whole. The simplest do-it-yourself unit testing takes about three lines of code to set up in whatever language you’re using. The test method goes something like this:

void assertEqual(int expected, int actual, String message){
if (actual != expected){
System.err.println("Assertion failed (" + message + "): expected " + expected + ", got " + actual);
}
}

This works even better in non-typed languages where you don’t have to make one version for ints and one for floats and so on. If you’re doing more serious development you should of course take the time to dig up a serious unit testing framework for you language (such as JUnit for Java).

The original processing code is here: Processing code - Limit utility

Update: DOH! I just found out that somebody already thought of this. There’s a Processing method contrain(value, min, max) that does the same thing!