Configuring Eclipse to use a JDK at a location with spaces in it

September 25, 2008

I was setting up my Eclipse environment with some new plugins today and ran into some trouble, which seems to have bothered many souls as I gather from the posts I found when trying to fix the problem.

Some tools/plugins/features in Eclipse may need to use a JDK rather than a JRE. Sometimes you may even wish to switch between different versions of the JRE or JDK being used since typically 1.4, 1.5/5 and 1.6/6 are all present on a developers’ system at different locations.

I got a mesage from a Maven plugin which said that it needed a JDK to be used to run Eclipse instead of a JRE that it was currently using. Well, this shouldn’t usually present much of a problem. You can choose the virtual machine used to run Eclipse by passing the -vm argument to eclipse.exe. What’s usually needed is something like “eclipse.exe -vm c:\jdk1.6\bin\javaw.exe”. Mind you, javaw.exe is used when you don’t want an associated console window.

I did have JDK 1.6 installed. But it was installed to “C:\Program Files\Java\jdk1.6.0_07″. That, it seems, is the default location. The space in the path caused big problems. I tried quotes\no quotes and other magical incantations to get it to accept the argument with a space but to no avail.

So instead of using the command line switch, I put an entry into eclipse.ini which is in the same directory as eclipse.exe. But this didn’t work either.

Finally, what did work was that I started command.exe (the 16 bit DOS shell) and navigated to the JDK directory using “dir /X” for help which shows names generated in the format “C:\PROGRA~1\JAVA\JDK16~1.0_0\BIN”. Perhaps you remember coming across such filenames.

Anyway, the older 8.3 style directory name worked! My final eclipse.ini is posted below.

-showsplash
org.eclipse.platform
–launcher.XXMaxPermSize
256M
-framework
plugins\org.eclipse.osgi_3.4.0.v20080605-1900.jar
-vm
C:\PROGRA~1\JAVA\JDK16~1.0_0\BIN
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m

I was using Windows XP with SP3 and Eclipse 3.4. There is likely to be a better solution that I missed. If you have it or if this post helped you, please leave a coment. :)


Java Synchronization Benchmark

February 1, 2008

Here is a very simple and straightforward benchmark to demonstrate how synchronization in Java can affect speed of execution to different extents in Java 1.4 and Java 6.

Java Synchronization Benchmark

In the attached image (click above for full size), you can see that for Java 1.4 the synchronized method needs over 700% more time to do its work compared to the non-synchronized method. But for Java 6, the difference is lower with the synchronized method needing only 400% more time to do its work compared to the non-synchronized method.

Remember, JVMs, especially newer ones do some nifty runtime optimizations that most developers are not aware of at all – like “biased locking”. This feature reduces the time taken for reacquiring a lock if the same thread is taking the lock repeatedly. There are other features like “adaptive spinning” and “lock coarsening” as well. Ofcourse, the fact is that it might just be other optimizations that are at work here instead.

Such a simple benchmark does not deserve to have any conclusions drawn from it. Infact the effect may have been greatly amplified here because the program does only an increment within the function so locking becomes the bottleneck. Such a situation may ofcourse be true in “real world” programs as well. But quite often, it is not.

I obtained this result on the single core 2.8Ghz PIV system at my desk at work.

Ideally, one should be using a multi-processor machine and multiple threads to demonstrate such effects. I would expect Java 5 to show results between those for 1.4 and 6.


Googling Sun Developer Network leads to Microsoft Visual Studio ad

December 9, 2007

I just Googled “Sun Developer Network”. Look what turned up! Hahaha! An ad for Microsoft Visual Studio 2008 which says “Defy All Challenges”. (Screenshot inserted at end of post)

It seems someone at MS Marketing realized that the realease of NetBeans 6 would be the best time to entice OpenSource platform users over to the MS side. Or atleast to make ‘em jealous with a shiny, Flash-y website. Looks like Microsoft bought itself some Google AdWords ads for that phrase. Take a look – Microsoft Visual Studio 2008 – Defy All Challenges. It’s a pretty high bandwidth kinda site though.

The IDE wars are starting to make things pretty cool for the developer community now with Sun pushing NetBeans, Microsoft pushing Visual Studio, IBM going with Eclipse and Oracle with JDeveloper. Check out some of the video demos of new NetBeans 6 features.

Googling Sun Developer Network leads to Microsoft Visual Studio ad


Azureus extension to schedule download speed by pinging an IP using Java

September 15, 2007

I have a 128Kbps Internet connection with no download caps. So I often leave torrents downloading huge files on my torrent client overnight or when leaving home. Problem is that this brings the net speed down to a crawl for my roomies who access the net through an HTTP proxy running on my PC. So I’ve also given them access to a web interface of the torrent client through their browser so that they could pause/resume the downloads as needed.

The problem here was that the buggers forgot to resume the downloads once they were done. And this happened almost every time they paused them.

So I decided to to do something about it. I wrote some Java code that pinged my roomies’ machine every few seconds and if there was no reply to the ping, it would resume the torrents at full speed. If the reply to the ping was received, it would do nothing. I didn’t immediately come across any Azureus Plugin that would allow me to do so.

How would one write code to do so? One way would be to get into the Azureus code base (it is open source) and see how to write a plugin. But doing that seemed too much for too little. A much quicker, but dirtier fix comes to mind. I installed the Firebug addon for Firefox. With this plugin enabled, I could see the exact contents of each HTTP – GET or POST request in real time as I used the browser. So I navigated to the web interface for Azureus on my own PC running at some thing like http://127.0.0.1:54323. Once there, I did what one would do to set maximum speed for downloads and noted the GET request sent by the browser to the Azureus web interface server.

Now in less than the time it took to write this post, I put together some Java code which would repeatedly ping my roomies’ machine and send the GET request to Azureus when the machine didn’t reply. Voila! All I had to do was ask my roomies to set the download speed to 1 KB/sec when they needed to use the net instead of pausing the torrent. (That could also be taken care of, but I was feeling lazy.)

Ofcourse, the same could be done for similar functionality with other clients that provide a web interface like uTorrent and eMule.

Here is the code that does the above. It is a dirty hack I threw together in 25 minutes at 2 O’ clock in the night. I usually write code much better. Do not comment on the quality or coding style. I know there are no worth while comments or intuitively named methods or variables. Go on and improve it if you wish. Point to be noted, it works.

import java.net.*;
import java.io.*;

public class AzureusSpeedSetter {

	void setSpeed()
	{

		try
		{
			URL u = new URL("http://127.0.0.1:6886/index.ajax?max_dl=1&max_active=2&max_active_seed=0&max_active_seed_enabled=0&max_conn_pertor=60&max_conn=400&max_dl_speed=0&max_ups=10&max_ups_seed=4&max_auto_up=0&max_ul_speed=12&max_ul_speed_seed=0&max_ul_speed_seed_enabled=0&comp_tab=0&date=" + new java.util.Date().getTime());
			HttpURLConnection conn =  (HttpURLConnection) u.openConnection();

			conn.connect();
			System.out.println(conn.getResponseMessage());
		}
		catch (IOException ioe)
		{
			ioe.printStackTrace();
		}
	}

	void monitor()
	{

		while(true)
		{
			try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException ie)
			{
				ie.printStackTrace();
			}

			if(isOffline()) {
				setSpeed();
			}
			else
				System.out.println("Is active.");
		}
	}

	boolean isOffline()
	{
		String line;
		try {
			BufferedInputStream br = new BufferedInputStream(Runtime.getRuntime().exec("ping -n 1 192.168.0.3".split("\\\\s")).getInputStream());

			byte[] buf = new byte[10000];
			int count  = 0;
			int offset = 0;

			while( (count = br.read(buf,offset,1000)) != -1 )
				offset += count;

			String s = new String(buf,0,offset);
			if(s.indexOf("Request timed out.") != -1)
				return true;
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

	public static void main(String[] args) throws Exception
	{
		AzureusSpeedSetter ass = new AzureusSpeedSetter();
		ass.monitor();
	}
}