Posts tagged ‘Windows’

Redirect a System Process into Java Application for Windows and Linux

The output or input redirection is often used by the command line script guys. The output redirection (“>”) is usually to the file instead of the standard output. The input redirection (“<”) is to have a file as input instead of the standard input. It is sometimes needed in the programs too.

Interoperability with other programs is another concept especially when developing interoperable application. To do that the programming framework often sports different libraries for interoperability. However sometimes, for a simple code you don’t want to call the low level heavy libraries for doing the operation while there is a simple workaround.

There might be three ways of doing it.

  1. Trusting to the environment shell or command prompt.
  2. Use the Java internal process libraries and do the redirection ourselves.
  3. Use JNI class libraries for doing cross platform operation with the native code.

In one of the projects I was working with one of the earthquake modules that does some calculations. The program is in compiled form that it won’t worth the reimplementation. Instead I needed to call from the environment and display the output.

In order to use the system command prompt it is needed to use the “exec” method of the Runtime. This solution might not be guaranteed to work if the command line is broken or it is not with the standard names. Also it is not really beautiful to have if statements detecting the operating system and behaving on that.

Runtime.getRuntime().exec(cmdCommand);

void creatproc()
{
	String osName = System.getProperty("os.name");
	Process p;
 
	if (osName.startsWith("Windows"))
	{
		String cmdwin = "cmd /c C:EWv6.2binhyp2000.exe < C:EWv6.2bininput.txt";
		try
		{
			File f = new File("C:EWv6.2bin");
			Runtime.getRuntime().exec(cmdwin, null, f);
		}
		catch (IOException e)
		{
 
			e.printStackTrace();
		}
	}
	else
	{
		String[] cmdlinux = new String[3];
		cmdlinux[0] = "/bin/sh";
		cmdlinux[1] = "-c";
		cmdlinux[2] = "hypo2000 < input.txt";
		try
		{
			Runtime.getRuntime().exec(cmdlinux);
		}
		catch (IOException e)
		{
 
			e.printStackTrace();
		}
	}
}

After detecting the operating system it for windows it is needed to call cmd and for Linux> it is needed to call sh. There is one implementation between the command prompt of those operating systems as well. Likewise Windows could tell the difference of a single string while in Linux you have to specify the command arguments as the arrays of string. Other than that everything works exactly the same as you would expect.

A better implementation would be to use the java.io.BufferedReader; and feed the process that we have opened with the file contents. It is like implementing the behind scenes in our Java code. We create the process and write to the process output while reading from the file.

public void Redirect()
{
	BufferedReader freader = null;
	PrintStream fout = null;
	java.io.PipedReader pRead = null;
	PipedOutputStream po = null;
	Process p = null;
	try
	{
		freader = new BufferedReader(new FileReader("C:EWv6.2bininput.txt"));
 
	}
	catch (FileNotFoundException e1)
	{
 
		e1.printStackTrace();
	}
	try
	{
		File f = new File("C:EWv6.2bin");
 
		p = Runtime.getRuntime().exec("C:EWv6.2binhyp2000.exe",
				null, f);
		fout = new PrintStream(p.getOutputStream());
	}
	catch (IOException e)
	{
 
		e.printStackTrace();
	}
	String line = "";
	try
	{
		while ((line = freader.readLine()) != null)
		{
			fout.println(line);
		}
		fout.flush();
	}
	catch (IOException e1)
	{
 
		e1.printStackTrace();
	}
	try
	{
		p.waitFor();
	}
	catch (InterruptedException e)
	{
 
		e.printStackTrace();
	}
}

I found the usage of Java process libraries better than the others, but for quick solution the others might be helpful as well. Also it doesn’t look good in terms of “portability” of the application code.

Compile LAPACK and BLAS as DLL on Windows

BLAS (Basic Linear Algebra Subprograms) is a library that provides standard routines for basic vector and matrix operations.

LAPACK is a library that provides functions for solving systems of linear equations, matrix factorizations, solving eigenvalue and singular value problems. LAPACK library have dependency to BLAS library because of the LAPACK functions using BLAS functions to work.

Both of those libraries are written in Fortran77 and no binaries provided for windows. The sources are downloadable from netlib site. So you can compile and use their libraries. However makefile and make solutions for compiling under windows are cumbersome.

So I recommend compiling directly from the sources using a FORTRAN compiler. There is a free and open source FORTRAN compiler for Windows which is the port of gnu FORTRAN compiler for Windows. You could also use Cygwin and tools in cygwin but your dynamic library loader will have dependency to cygwin dlls. MinGW is a collection of tools that allows you to produce native Windows programs. G77, make, gcc are the common tools provided by Mingw.

Here are the steps to go to compilation

  • Download most current version of LAPACK from Netlib and extract the sources
  • Download almost all of the Mingw tools and extract the tools
  • Copy dlamch.f and slamch.f from INSTALL directory SRC directory
  • Set path to have mingw binaries
    • set PATH=c:\mingw\bin\;%PATH%
  • Go to root directory of the extracted folder and compile using g77
  • First compile BLAS. –shared option is needed in order to functions to be exposed from the dll. -O generates optimised code. -o filename is the output file
    • g77 –shared -o blas.dll BLAS\SRC\*.f –O
  • Compile LAPACK with BLAS dependency
    • g77 –shared -o lapack.dll src\*.f blas.dll -O

Here is the output:

c:\\lapack-3.1.1\\copy INSTALL\\dlamch.f SRC\\dlamch.f
       1 file(s) copied.
c:\\lapack-3.1.1\\copy INSTALL\\slamch.f SRC\\slamch.f
       1 file(s) copied.
c:\\lapack-3.1.1\\set PATH=c:\\tools\\mingw\\bin\\;%PATH%

c:\\lapack-3.1.1\\g77 --shared -o blas.dll BLAS\\SRC\\*.f -O

c:\\lapack-3.1.1\\g77 --shared -o lapack.dll src\\*.f blas.dll -O

Hope this helps.

Silverlight and DLR

SilverlightBack to my previous post, WPF/Everywhere recently become Silverlight with the addition of new languages and new platform. Actually it’s not a new platform it’s a subset of IronPhyton programming language and it’s called DLR Dynamic Language Runtime. So more to .NET static typed languages, we have some new dynamic languages to be used for scripting in Silverlight. Moreover they will support IronPhyton, IronRuby, JavaScript and VB as dynamic language. It is still possible to use the Silverlight to code using the class library.

I think this is a different move and strategy from Microsoft to target more platforms with completely new and immature languages. However it is very promising in terms of technology it provides, cross platform cross browser flash like environment for different programming languages. We had .NET in different platforms like ASP.NET in the web server, Windows Forms and WPF in Windows and now we have .NET in the browser. You don’t have to have .NET framework to be installed because it comes with a small subset of .NET Framework in the plug-in.

So what it adds more browsing experience is of course the usage of .NET languages and a subset of .NET framework. It means that we can write multithreaded Javascript or C# in the browser plug-in. Different than server client model, the application executes in the client and uses their resources. The silverlight plug-in watches for user interaction and a call-back is being sent to the plug-in or to the browser. JavaScript serialisation, JASON, and marshalizing stuff are handled by Silverlight as well. It allows using asp.net style declarative programming, LINQ and WPF in the browser.

The platforms officially supported are Mac and Windows of course. In a very early stage (in 2 weeks after release) Mono has been released their implementation of Silverlight called Moonlight. This was quick because of the open source and BSD style license of Silverlight. So it is becoming a real cross platform in that case, just like flash does with more programming languages and techniques.

Silverlight is very good at advertising as well. A new developer platform space for sharing Silverlight projects is open, PopFly. It is the area to put silverlight applications and see what is possible with Silverlight. Moreover it is also possible to get 4GB free hosting space from Microsoft Silverlight Streaming. However we don’t know how much they will charge later on.

Silverlight might compete with flash in very short time. Flash is de facto standard for most of the browsers.A lot of web applications run flash to provide rich client features, now Silverlight come to the area with the power of .NET framework and your favourite languages.

Internet with GPRS over Bluetooth using Window Vista

Finally I moved, but I have a little problem “no internet connection”.

Let’s agree that we are addicted to online world (at least I am). It has been 2 days I haven’t got the connection. I was using my phone to check mails and than realised to use with my laptop. So I set up a GPRS connection over Bluetooth from my laptop using my phone.

I set the browsers not to download images and e-mail clients to download only the headers of the mails to save some bytes.

Thanks to Windows Vista for making the Bluetooth connection that simple. I remember old days for finding the modem drivers for my phone and dial a *#1 combination to connect to the Internet. Now in a single window, I am on the internet.

First add your device to Bluetooth devices, than connect and handshake with your phone using Bluetooth.

Bluetooth Connection

Bluetooth Devices

Go to Control Panel\Network and Internet\Network Connections; you will see Personal Area Network.

Network Connections

Open its properties and “Connect”.

Bluetooth Connect

Done!.

Bluetooth Status

Secure Remote Desktop on Linux and Windows

Remote Desktop is one the great features added to Windows since XP.  From then many clients exists for connecting to remote desktops including linux, Mac OS X. Remote desktop connection uses Remote Desktop Protocol (RDP) and the protocol has 128 bit encryption; however it is possible to decrypt entire connection because of its implementation.  We need to use some other layers to make the connection secure

TLS/SSL can be used to secure the connection but it is for server systems. Also for cross platform issues it might not be the best solution. Linux’s famous secure desktop shell (ssh) can be used for such purpose. SSH is not just a remote shell, more importantly it provides TCP tunneling and port forwarding with the built-in encryption of course.

You need to have an ssh server either on windows or linux machine that is accessible from the outside world. For remote connections normally you need to open port 3389, in this case only ssh server port needs to be open from the firewall. For windows ssh server OpenSSh for Windows might be a good choice for client and server. Simply you need to install ssh server and add users to the server.

What you need to do is to logon the remote system and ask the system to redirect you to a machine with the port number. As a result you get an encrypted tunneled connection to your remote machine.

can@host-174-92:~> ssh -L 3389:192.168.1.111:3389 -C 112.232.121.111 -l can
The authenticity of host ’112.232.121.111′ can’t be established.
RSA key fingerprint is 47:da:4e:ab:94:2b:d7:39:cc:19:17:33:55:6a:73:61. Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ’112.232.121.111′ (RSA) to the list of known hosts.
can@112.232.121.111′s password:
Last login: Fri Oct 13 15:49:55 2006 from x

can@host-174-92:~> rdesktop -u username -a 24 localhost

.

Linux

On linux you use the remote desktop client rdesktop for connection remote desktop servers. Since the redirection is done to localhost with the previous command. Connection to local rdesktop will actually connect us to the remote.

can@host-174-92:~> rdesktop -u username -a 24 localhost

.

logon

Remote Desktop

Visual Studio

Windows

In windows the process is almost the same unless you have a Windows XP Pro or Vista Ultimate Editions. The problem with those versions is that they have a remote desktop server running on port 3389. In that case, you need to tunnel through an unused different port. For instance 3390 should be available. The only thing you will change is the port connection to the ssh server.

openssh -L 3390:192.168.1.111:3389 -C 112.232.121.111 -l username

.

With this command you redirect your system’s 3390 port to remote system’s 3389 port. Of course the ports must be the server’s port. After that we just connect using Windows Remote Desktop client with the adress “localhost:3390″. We will be connected to the remote host than after.

Conclusion

As a result on your corporation, the only port to open to the outside world is the ssh server port which is usually port 22. From that you can redirect every traffic to an internal machine using secure connection and tunneling. This is not limited to remote desktop connections of course, you can use some other services or protocols to make them more secure.  

Windows Sideshow and Gadgets

Windows Vista new features has been started to implement by many hardware manufacturers. One of the features that I want to use mostly is Windows Sideshow.

The Windows Sideshow site points that :

Wouldn’t it be great if you could read an e-mail message, confirm a meeting location, or check a flight reservation without turning on your computer?

I really want to do that without switching on my computer. I don’t want to load the operating system just for checking mails. Windows Sideshow is basically a gadget interface. For the moment some interesting products are using  Windows Sideshow technology like motherboards, notebooks, bags. I mostly liked sideshow enabled notebook. I am sure there will be more interesting products in a wide range later this year.

With the adoption of Windows Sideshow, gadgets will be more important which will make Windows Sidebar more important product  among other gadget engines. Gadgets are small programs for many different purposes. Mostly they are programmed in XHTML and javascript. If you have an idea for a gadget programming them is not a big deal. I mostly found the gadgets I needed and hacked some of them to work much better :) . Currently there are a few hundred gadgets in the Windows Gadget Gallery and is increasing everyday. I personally like the gadget idea although I prefer to use Yahoo Widget Engine because of the heads up display and the number of widgets. I use gadgets for e-mails, weather, system monitoring, alarms,  TV schedules, and some RSS feeds that I can ignore.