Archive for the ‘java’ Category.
20th July 2008, 12:50 am
I was using Eclipse for one for the previous projects as a development environment. It has some powerful features. But I have to say not as powerful as Visual Studio. Anyway since it is possibly the best IDE for Java development it was my choice on the day of implementation.
The project that I was working was done using Borland’s Jbuilder. Unfortunately that IDE doesn’t exist anymore as it is discontinued by a new company. Thanks to Eclipse import project wizard, the project migration wasn’t difficult at all.
I just opened the project XML and the project was ready to be working. However, when using with different big open source libraries, Eclipse has a problem in managing them. Actually I have a problem in managing them, because I need to find the required version of each dependency and so on. It is the difficulty of package and build management of eclipse.
It is often difficult to reference a library and get all the dependencies on Eclipse. Somehow it doesn’t warn at compile time as well and you get exception at runtime which is not nice enough.
Than I found a project called Maven for doing proper build systems. What it does is has an online repository that have all the dependencies filed and it downloads the required assemblies for your project. So you don’t have download each package separately and link to project. All the best it is very well integrated with eclipse with its marvelous plug-in.
You load the plug-in and tell maven to add a reference to your project. I know Java programmers call this as something else but since I’m mainly a .NET programmer I hope somebody will find it useful.
Anyway, it stores the configuration in XML file that you can tweak it later if needed. All the updates to the references can happen automatically as well which is quite nice.
21st December 2007, 03:18 pm
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.
- Trusting to the environment shell or command prompt.
- Use the Java internal process libraries and do the redirection ourselves.
- 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.