Thursday, July 31, 2014

Wednesday, July 16, 2014

Executing batch file through Java

Batch file has to perform below actions.

1. Change the directory for a given path
2. start the node sever
3. Exit the command window

>cd D:\nodev0.18
>node app.js console
>exit

FileName: Myfile.bat
Batch file defined with variable arguments like below.

cd %1
%2 app.js console
exit

Java code for invocation:
String executePath = "cmd /c start Myfile.bat"
   Runtime.getRuntime().exec(executePath);

Executing windows commands through java

I wanted to start the node server programmatically.
>node app.js


Using Java:

String executePath = "cmd /c start node app.js"

Runtime.getRuntime().exec(executePath);


Removing eclipse view title bar tab


public class Perspective implements IPerspectiveFactory {
  public void createInitialLayout(IPageLayout layout) {      
    String editorArea = layout.getEditorArea();
    layout.setEditorAreaVisible(false);

    layout.addStandaloneView(View.ID, false, IPageLayout.LEFT, 1.0f, editorArea);      
  }
}


A standalone view's title can optionally be hidden. If hidden, then any controls typically shown with the title (such as the close button) are also hidden. Any contributions or other content from the view itself are always shown (e.g. toolbar or view menu contributions, content description).

Source:
http://andydunkel.net/eclipse/java/swt/2011/10/04/eclipse-rcp-remove-tab-folder-from-view.html

Thursday, July 3, 2014

Setting "java.library.path" programmatically

Requirement:

I have 6 dll files in my eclipse plug-in under lib/win64 folder. I wanted to load them dynamically during the launch of eclipse.

I have tried the eclipse way of bundling native-code in the plugin manifest, but somehow i could not achieve. So I have to go by manipulating the system path.

Eclipse bundle-native code mechanism:
http://blog.vogella.com/2010/07/27/osgi/


try {
     //eclipse call
Bundle bundle= getBundle();
URL fileUrl = FileLocator.toFileURL(FileLocator.find(bundle, new Path("/"), null));
String location = fileUrl.getFile();
String libPath = location + "lib/win64";
File file = new File(libPath);
System.out.println(file.getAbsolutePath());
System.setProperty("java.library.path", file.getAbsolutePath());
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible( true );
fieldSysPath.set( null, null );
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


The Classloader has a static field (sys_paths) that contains the paths. If that field is set to null, it is initialized automatically. Therefore forcing that field to null will result into the reevaluation of the library path as soon as loadLibrary() is called

Resources:
http://blog.cedarsoft.com/2010/11/setting-java-library-path-programmatically/
http://fahdshariff.blogspot.in/2011/08/changing-java-library-path-at-runtime.html

Generate java classes from xsd using JAXB

We can use JAXB to do this job.

More info:
https://jaxb.java.net/

JAXB bundles with JDK kit from 1.6 onwards.

You can find a service called 'xjc' in the <jdk directory>/bin folder.

Example:
C:\Program Files\Java\jdk1.6.0_30\bin>xjc D:\Modules\functionalModules.xsd
parsing a schema...
compiling a schema...
generated\FunctionalModule.java
generated\FunctionalModules.java
generated\ObjectFactory.java

Java classes will be generatd with in the 'generated' folder of java bin directory.

Friday, May 16, 2014

Removing untracked files from local git branch

We can use git clean command to remove all untracked files from your local working branch.
> git clean -f

This will remove all untracked files forcefully.

To remove even the untracked directories which are present in the local branch.
>git clean -f -d