Category Archives: java


SSH keys are a way to identify trusted computers, without involving passwords.


To connect remote machines through SSH key using java and running the command in remote.


Step1: We need to generate an SSH key in our computer. Generate SSH Key
Step2: We need to add these private/public keys from the ssh folder to remote.
Step3: Authenticate SSH key in the particular remote using Java.

  • Create SSHClient instance.
  • Get the known_hosts file under ssh folder and load these hosts under ssh client.
  • Connect to the host, which is based on requirement.
  • Based on OS, get the private/public keys under ssh folder in our computer.
  • Get the pass phrase which is entered while generating the ssh key and load all the keys and add them to SSHClient.

To connect remote using ssh key and run any command in remote.

First we need to add ssh Jar to our project.
Download from Repo: SSh Java Jar File
Use Maven dependency in pom.xml:


How to clear all files in the particular folder in remote using java through ssh connection.

package com.nbos;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;

public class SSHConnector {

	public SSHClient authenticateSSH(String host, String sshUserName, String sshPassphrase) {
		try {
			final SSHClient ssh = new SSHClient();

			String username = System.getProperty("");
			String nameOS = "";
			String OS = System.getProperty(nameOS);
			File privateKey = null;

			if (OS.contains("Windows")) {
				File known_hosts = new File("C:\\Users\\"+username+"\\.ssh\\known_hosts");
				privateKey = new File("C:\\Users\\"+username+"\\.ssh\\id_rsa");
				KeyProvider keys = ssh.loadKeys(privateKey.getPath(), sshPassphrase);
				ssh.authPublickey(sshUserName, keys);
			} else if (OS.contains("Mac")) {
				File known_hosts = new File("/Users/"+username+"/.ssh/known_hosts");
				privateKey = new File("/Users/"+username+"/.ssh/id_rsa");
				KeyProvider keys = ssh.loadKeys(privateKey.getPath(), sshPassphrase);
				ssh.authPublickey(sshUserName, keys); 
			} else {
				privateKey = new File("/Users/"+username+"/.ssh/id_rsa");
				KeyProvider keys = ssh.loadKeys(privateKey.getPath(), sshPassphrase);
				ssh.authPublickey(sshUserName, keys); 
			return ssh;
		} catch (Exception e) {
			System.out.println("exception in execRedemption ");
			return null;

	public Boolean execRemoteCommand(String host, String CMD, String sshUserName, String sshPassphrase) {
		final SSHClient ssh = authenticateSSH(host, sshUserName, sshPassphrase);
		try {
			final Session session = ssh.startSession();
			final Command cmd = session.exec(CMD);
			cmd.join(30, TimeUnit.SECONDS);
			InputStream in = cmd.getInputStream();
			byte[] tmp = new byte[1024];
			while (in.available() > 0) {
				int i =, 0, 1024);
				if (i < 0) {
				System.out.print(new String(tmp, 0, i));
			try {
			} catch (Exception ee) {
			System.out.print("\n** exit status: " + cmd.getExitStatus());
			return true;
		} catch (Exception e) {
			System.out.println("exception in execRemoteCommand ");
			return false;

	public static void main(String[] args) {
		SSHConnector sc = new SSHConnector();
		Scanner sca = new Scanner(;
		System.out.println("********* Enter Host Name *********");
		String host =;
		System.out.println("********* Enter ssh username *********");
		String sshUserName =;
		System.out.println("********* Enter ssh passphrase *********");
		String sshPassphrase =;
		System.out.println("********* Enter any command *********");
		String cmd =;
		sc.execRemoteCommand(host, cmd, sshUserName, sshPassphrase);

Solr provides ability to customize handling of Solr requests by extending built in or by writing completely new Request handlers and Search Components. Search components define the logic for running Solr queries and make the results available in the response which will be returned to users. There are several default search components that work with all SearchHandlers without any additional configuration and these are executed by default, in the following order

Screen Shot 2015-09-08 at 10.16.11 am

If you register a new search component with one of these default names, the newly defined component will be used instead of the default. The search component is defined in solrconfig.xml separate from the request handlers, and then registered with a request handler as needed. By default Solr uses a search component named “query” for performing search operations.  Below sample configuration is for overwriting default search component with a new search component ‘MyQueryComponent’.


Executing multiple search queries

By default QueryComponent can execute one Solr search per request. For supporting multiple Solr queries we need to write a search component either by extending QueryComponent or SearchComponent.

Write a class by extending QueryComponent

Overwrite prepare() and process() methods to customize our needs.

prepare() method:

We can write logic for validating request and prepare component for processing queries.  The custom query component we are writing do not support grouping and validating mandatory query parameters.

process () method:

Process method first extracts raw solr queries (buildRawQueries())  and build SolrParams (parseRawQueries) out of raw Solr queries. Each SolrParams instance resembles one query.  Execute one query at a time and the query result is added to ResponseBuilder. The logic for query execution is delegated to QueryComponent as there is nothing much we need to override for our requirement (processQuery).  Queries are executed in order specified and until we retrieve max rows required.  If rows parameter is passed -1 all queries are executed.

Prepare the final result by merging all the results of all queries executed and return to user.

Sample Solr query:


qt  :   Name of query handler that process multi query Solr request .
qn :   Number of Solr queries in the request
mQryX: Solr query which can be executed independently. Where X in parameter name should be an integer which denotes the order of execution of query. If ‘qn’ parameter is 4, we have to pass mQry1, mQry2, mQry3 and mQry4 parameters.
debug:  Defaults to false. If passed true Solr query debug information is returned in response.
sort:  Default Sort parameter if sort parameter is not available in individual queries (mQryX).

Register custom search component in solr  by adding the following in solrconfig,xml

MultipleQueryComponent has to be register with RequestHandler to be used. We can configure a new request handler using existing StandardRequestHandler or write a custom request handler.

Use built-in request handler and configure MultipleQueryComponent:

As a custom request handler:

Code of MultipleQuerySearchHandler:

Complete code of MultipleQueryComponent:

Handling HTTP 413 error status from Solr while executing multiple Solr queries.

When sending multiple queries in one request, Solr may return HTTP 413 status code.

Solr returns HTTP 413 status code when the client sends Solr queries in GET request and total number of characters are more than allowed.  The maximum number of characters allowed depends on server configuration Solr is running. Mostly all servers allow 1024 characters in GET request. To avoid HTTP 413 error, clients should send Solr queries using POST method when request payload contains more than 1024 characters.