Switching easily between Java JDKs on Mac OS X

April 17, 2017 Leave a comment

Quickly switching between multiple java versions on Mac Terminal

Wim Deblauwe's Blog

I just came upon a neat little trick to easily switch what JDK you are using on the command line on Mac OS X (10.9.2 is the version I am using), courtesy of this AskDifferent answer.

First, add the following aliases in ~/.profile file:

alias setJdk6='export JAVA_HOME=$(/usr/libexec/java_home -v 1.6)'
alias setJdk7='export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)'
alias setJdk8='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)'

Open a new terminal window or use source ~/.profile to load the aliases.

Now you can easily switch between the installed JDKs:

> setJdk6

We can verify that Maven picks this up correctly:

> mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)
Maven home: /Applications/maven/apache-maven-3.0.4
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.9.2", arch: "x86_64", family: "mac"

To go to JDK 7:

> setJdk7

And Maven shows:

> mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)

View original post 90 more words

Categories: Uncategorized

error: RPC failed; curl 56 SSLRead() return error -9806 fatal: The remote end hung up unexpectedly fatal: early EOF

April 17, 2017 Leave a comment

I came across this error when cloning a large git repository using a WiFi connection.

One quick setting to help overcome is to increase the http.postBuffer size in git and restart the cloning process. Use following command to increase the postBuffer size.

git config –global http.postBuffer 8M

In my case after trial and error I adjusted it to 8M. This may vary depending on the data which is pulled.

http.postBuffer from git documentation

Maximum size in bytes of the buffer used by smart HTTP transports when POSTing data to the remote system. For requests larger than this buffer size, HTTP/1.1 and Transfer-Encoding: chunked is used to avoid creating a massive pack file locally. Default is 1 MiB, which is sufficient for most requests.For reference – Git documentation link to various git config options

Git documentation for more git-config options – git-config documentation

 

Categories: Uncategorized

Retrieve passwords from FileZilla

April 23, 2014 1 comment

FileZilla client stores connection credentials in plain text. Following are the steps to recover lost FTP credentials from filezilla in Windows 7

Step 1: Open folder C:\Users\<YourName>\AppData\Roaming\FileZilla (If FileZilla is not found in Roaming folder, search it in other folders of AppData )

Step 2: Open file recentservers.xml. This file will contains the information all the recent connections made through file zilla along with passowords.

That’s it. By now we should have retrieved our lost passwords.

 

Categories: Uncategorized Tags:

Automatic requests being fired while executing a JAX-WS web service asynhronous call

June 17, 2013 Leave a comment

Further to my earlier post about how to send asynchronous requests using jax-ws. There might be an issue with long running server side operations when an asynchronous call is invoked. The issue could be that the asynchronous request the user has sent can automatically get triggered again after some point in time, causing undesirable effects on the server side.

Going deep and trying to understand the problem, I observed that the same client was sending a new request which was a replica of the original request. This was causing the server side operations to run again, and was giving undesired results.  On searching the web I came across this bug from Sun website.

SOAP request’s in the raw form are sent as HTTP post requests. In Java (JDK) implementation of POST, when a POST request is sent using sun.net.www.http.HTTPClient, the client waits for a fixed amount of time to receive a response from the server. If no response is received during the stipulated time it automatically fires the same request to the server, and JDK implementation is programmed with this assumption. Since POST requests are non idempotent (One which modifies database or server side when performing an operation Eg: POST, PUT, DELETE http methods) it may have undersirable effects in some conditions and the user may not want the request to be retried again by HTTPClient.

JDK provides a flexibility to the user to avoid the default auto retry behavior. This behavior is controlled by using the property

sun.net.http.retryPost

which is by default set to true. If we want the default retry behavior to be disabled, just set

sun.net.http.retryPost=false

in the system properties (-D sun.net.http.retryPost=false).

Note: Setting the property programmatically using System.setProperty(“sun.net.http.retryPost “, false) may not work as the value is initialized in the static block in the HttpClient class of JDK. More details in the reference section.

References:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6382788

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/net/www/http/HttpClient.java

http://www.coderanch.com/t/490463/sockets/java/Timeout-retry-URLHTTPRequest

Categories: web services Tags: ,

How to make asynchronous call to SOAP Web Service using JAX-WS

June 8, 2013 2 comments

Before I show an example of how to invoke asynchronous web service using JAX-WS let’s take a brief know how about synchronous and asynchronous calls.

Synchronous Web service call : A program calling the web service sends a request to the web service and waits till the web service returns the response, before executing the next statement.

Asynchronous Web service call : A program calling the web service sends a request to the web service and proceeds with the execution of remaining statements even before the web service has returned any response.  (This may be because a web service might take time to compute and generate the response, and the client doesn’t want to wait till it receives the response)

I was trying a scenario where I send a web service request and was not interested in the response. The web service would usually take a longer time to process and because of this the program execution in the client was stuck at the web service invocation call. This usually is the case with any of the synchronous call. I just wanted to send the request to the web service and proceed with execution remaining steps in the client without waiting for the web service to return.

Example of how to make asynchronous calls to JAX-WS Web service

1) Create a sample JAX-WS Web service and publish it

@WebService
public class SimpleSOAPServiceImpl{

	@WebMethod
	public String sayHello(@WebParam(name = "echo") String echo) {
		try {
			Thread.sleep(1000*60*5); // Sleep for 5 minutes
			return "Hello "+echo;
		}
		catch(InterruptedException ex) {
			// Exception Handling code here..
		}
		return null;
	}

	public static void main(String args[]) {
		Endpoint.publish("http://localhost:1234/MySOAPService", new SimpleSOAPServiceImpl());
	}
}

2) Create a binding xml to be used with wsimport

<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->

        <!-- Applies to all the methods in the wsdl. If set to true all the methods can be          invoked asynchronously -->
        <enableAsyncMapping>true</enableAsyncMapping>

        <!-- Applies asynchronous binding only to selected methods -->
        <!-- Uncomment below binding section, when the above 'enableAsyncMapping' tag         is set to false -->
        <!--
        <bindings node="wsdl:definitions/wsdl:portType[@name='SimpleSOAPServiceImpl']/wsdl:operation[@name='sayHello']">
     		<enableAsyncMapping>true</enableAsyncMapping>
 		</bindings>
 		 -->

3) Run wsimport to generate client artifacts. (I ran from command prompt)

D:/> wsimport http://localhost:1234/MySOAPService?wsdl -b asyncBinding.xml -s .

4) Check the generated interface after running wsimport. This will have 2 extra methods(sayHelloAsync) for each method which was specified in the binding file

// Asynchronous Method
public Response sayHelloAsync(
		@WebParam(name = "echo", targetNamespace = "")
		String echo);

// Asynchronous Method
public Future<?> sayHelloAsync(
        @WebParam(name = "echo", targetNamespace = "")
        String echo,
        @WebParam(name = "asyncHandler", targetNamespace = "")
        AsyncHandler asyncHandler);

// Synchronous Method
public String sayHello(
        @WebParam(name = "echo", targetNamespace = "")
        String echo);

5) Invoke the web service from client

public class Client {
	public static void main(String args[]) throws Exception {
		SimpleSOAPServiceImplService service = new SimpleSOAPServiceImplService();
		SimpleSOAPServiceImpl port = service.getSimpleSOAPServiceImplPort();

		port.sayHelloAsync("This is Asynchrous call.. ");
		// port.sayHello("This is synchronous call");

		System.out.println("Execute this statement before async call is complete.. ");

	}
}

The above client prints Execute this statement before async call is complete.. even before the web service actually finishes processing.

The beauty of making asynchronous calls from JAX-WS is that the web service need not be modified to support any asynchronous calls and remains as is. All magic happens at the client side.

Following reference document details the usage of making asynchronous calls using JAXWS

References

https://today.java.net/pub/a/today/2006/09/19/asynchronous-jax-ws-web-services.html

Categories: web services Tags: ,

Path Vs Classpath

As a Java Developer one frequently encounters these terms but often gets confused when to use what.

Here I found an informative article explaining both: http://geek.starbean.net/?p=38

Note: Following content is reflected from the above article.

Still a lot of people don’t understand the purpose of these system variables. They don’t know when to set it, what it really does and therefore cannot determine that this IS or IS NOT a path or classpath problem.

The PATH variable is a Windows system variable that Windows uses to find executable files. When you type a command in the cmd window, e.g. “javac”, Windows will search in your PATH to find the program to execute. The search sequence is as follows:

Current Dir > PATH Variable > Command Interpreter

In the example, the interpreter will first look in the Current Dir for a javac.com, javac.exe or javac.bat respectively. If found, it will be executed. If not it continues its search through the PATH variable in definition sequence and extension sequence. This means for PATH=C:\Temp;C:\Windows, C:\Temp\javac.com will be searched 1st, followed by C:\Temp\javac.exe, C:\Temp\javac.bat, C:\Windows\javac.com, and so on.

Once the PATH is exhausted, the interpreter will be checked if it is a interpreter command (such as “copy”). If so it will be invoked. Finally the interpreter will return a “Bad command or file name” if the command is not found.

All these means that if you’re getting a “Bad command or file name”, it is likely a PATH problem. It also means if you’re somehow running the wrong program, it may be a program of the same name in a path earlier than yours (useful for java version conflicts).

The CLASSPATH variable is a Java runtime system variable that Java uses to find class files. When Java tries to load a class, it will lookup its ClassLoader. If the class is not yet loaded, Java tries to find the class in the CLASSPATH variable. Sometimes the CLASSPATH may be appended from the runtime command line directly.

This means that if you’re getting a ClassNotFoundException during runtime, or “Unable to resolve symbol” during compile, you may have a CLASSPATH problem. (NOTE: Typos and other errors may also raise same compile error). If the classes are in the current directory and still cannot be found try adding “.” (current dir) to the classpath or check if the sources are in java packages.

To check the current PATH on cmd, type “PATH” or “set PATH”. To check the CLASSPATH, type “set CLASSPATH”.

To fix the PATH or CLASSPATH, you can either set it on the command line directly or set it through System Environment Variables. To set it from cmd, type “set CLASSPATH=someClassPath”. To append to the existing PATH or CLASSPATH, use %xxx%. E.g. “set CLASSPATH=%CLASSPATH%;.” to append the current directory into the classpath without affecting the existing classpath. Setting the variables on cmd only persists for that session. If you start another cmd or close this cmd the variables will be reset.

To persist the variables permanantly, you can set it through System Environment Variables. For XP Pro, go to the “System Properties” first. You can get there by

  1. Start > Control Panel > System
  2. Start, right-click “My Computer”, select “Properties”
  3. Press [WindowsKey]+[Break] simultaneously

with option 3 being the fastest. From there, click the “Advanced” tab and click “Environment Variables” at the bottom. Set either the User variables or System variables. The User variables only affect your Windows Login, and System variables will affect everyone. User variables will overwrite any System variable settings.

 

 

Categories: Uncategorized Tags: ,

Objects in Java are passed by value

December 19, 2012 Leave a comment

I often used to get confused with the behavior of java language when passing primitives method arguments Vs passing Array or Objects as method arguments. In this post I analyze the behavior of passing arguments types to the methods:

Let’s start with this example by passing primitives to as the method arguments:

Example 1:

public static void main(String[] args) {
int x = 10;
int y = 10;
add(x, y);
System.out.println("x = "+x); // Prints 10
System.out.println("y = "+y); // Prints 10
}

public static void add(int x, int y) {
x = x+10;
y = y+10;
System.out.println("x = "+x); // Prints 20
System.out.println("y = "+y); // Prints 20
}

As we see in the above example the primitive values are passed by value and the actual primitive values doesn’t change in the main method and they still print the original values.

Now let’s take an example of passing Objects as the method arguments and see the behavior

Example 2:

public static void main(String[] args) {
StringBuffer strBuf = new StringBuffer();
add(strBuf);
System.out.println("strBuf = "+strBuf); // Prints Hello World !!!
}

public static void add(StringBuffer strBufCopy) {
strBufCopy.append("Hello World !!! ");
System.out.println("strBuf = "+strBufCopy); // Prints Hello World !!!
}

Here the actual value of ‘strBuf’ varaible in the main method got changed after the modification in the add method. This makes us think “is StringBuffer (Objects) passed by reference” ?

Lets take another example by passing an Array as the method variable.

Example 3:

public static void main(String[] args) {
int[] integerArray = {1,2,3};
add(integerArray);
for(int i : integerArray) {
System.out.print(i+","); // Prints 100,100,100,
}
}

public static void add(int[] intArray) {
for(int i=0; i<intArray.length; i++) {
intArray[i] = 100;
}
for(int i : intArray) {
System.out.print(i+","); // Prints 100,100,100,
}
}

Again the actual value of integerArray got changed in the main method. Is Array passed by reference ?

Java strictly uses pass by value. Lets see the explanation:

Following is the way the above three examples work:

Example 1: In case of primitives a separate copy of values (x,y) was made and passed to the method, and the method was manipulating the copy of the primitive not the actual primitive. Thus the primitive values in the main method remains unaffected after calling the add method.

Example 2: It’s a different story when we pass an Object reference as the method argument. The object reference is still passed as Pass by Value, but refers to the same StringBuffer object created in the main method. Now we have two different object references ‘strBuf’ and ‘strBufCopy’ referring to the same StringBuffer Object. Now when we append a value using strBufCopy reference we are still referring the same old StringBuffer object created in the main method. Hence any change would directly refer to the actual object. This is the reason why we get Hello World !! printed in both strBuf and the strBufCopy.

Example 3: The explanation for this is same as Example 2, hence arrays in java are nothing but objects.

With the above example we conclude that Java is strictly pass by value.

Categories: Programming, Technical Tags: