Showing posts with label Jar File. Show all posts
Showing posts with label Jar File. Show all posts

Tuesday, June 23, 2015

JSch - Upload/download files from remote server

We will using JSch to connect our unix box. Once connected you should be able to upload or download the required files.

JSch is a pure Java implementation of SSH2. To know more click here.

For running below code you need to add jsh jar file (e.g. jsch-0.1.53.jar) in your applications classpath. Download jsch-0.1.53.jar from here.

Please see the self explanatory code below -


Saturday, February 22, 2014

Java : Encode/Decode Base64

We will be using Apache Commons codec to Encode and Decode Base64 data.

Apache Commons Codec (TM) software provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs. To know more, click here.

To run below example you will need to add commons-codec.jar in your classpath. Download commons-codec-1.4.jar.

Please see the self explantory java code below.

Wednesday, January 29, 2014

Thursday, July 4, 2013

Caused by: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

Exception in thread "main" java.lang.Exception: Error Initializing Context: oracle.jdbc.driver.OracleDriver
at com.hewitt.appinv.sdc.db.LocalContextFactory.createLocalContext(LocalContextFactory.java:28)
at com.hewitt.appinv.sdc.db.TestJNDI.main(TestJNDI.java:14)
Caused by: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.hewitt.appinv.sdc.db.LocalContextFactory.createLocalContext(LocalContextFactory.java:23)
... 1 more

If you see above error, Please add classes12.jar  in your applications classpath. Download it from here. classes12.jar  

Monday, December 10, 2012

Lucene - Updating index for an existing file


Updating index files could mean below two possibilities -
  • Adding a new file to existing index
  • Updating an existing file

Adding a new file to an existing index

Adding a new file is very simple. Please see my previous article. click here.

Updating an existing file

When you do IndexWriter.add() for a document that is already in the index it won't overwrite the previous document instead it will add multiple copies of the same document in the index.

There is no direct update procedure in Lucene. To update an index incrementally you must first delete the documents that were updated, and then re-add them to the index. In this example we will see how to delete a file and then you can re-add the same file with the help of my previous article. click here.

How to delete a documents from the index?

IndexWriter allows you to delete by Term or by Query. The deletes are buffered and then periodically flushed to the index, and made visible once commit() or close() is called.

IndexReader can also delete documents, by Term or document number, but you must close any open IndexWriter before using IndexReader to make changes (and, vice/versa). IndexReader also buffers the deletions and does not write changes to the index until close() is called, but if you use that same IndexReader for searching, the buffered deletions will immediately take effect. Unlike IndexWriter's delete methods, IndexReader's methods return the number of documents that were deleted.

Generally it's best to use IndexWriter for deletions, unless 1) you must delete by document number, 2) you need your searches to immediately reflect the deletions or 3) you must know how many documents were deleted for a given deleteDocuments invocation.

If you must delete by document number but would otherwise like to use IndexWriter, one common approach is to make a primary key field, that holds a unique ID string for each document. Then you can delete a single document by creating the Term containing the ID, and passing that to IndexWriter's deleteDocuments(Term) method.

Once a document is deleted it will not appear in TermDocs nor TermPositions enumerations, nor any search results. Attempts to load the document will result in an exception. The presence of this document may still be reflected in the docFreq statistics, and thus alter search scores, though this will be corrected eventually as segments containing deletions are merged.

To know more, click here.

Monday, April 30, 2012

Java: File searching program

To search a particular file recursively

Let’s see a Java program which will search a particular file recursively. In this example we will provide a root path from where it will search a particular file recursively. Either you can search a file by its name or you can provide a valid regular expression.

Using this logic you can implement a file searcher program.

About the example:

Logic behind this program is very simple; it searches a root folder recursively. If the current path is a directory then it will call the same function searchFile(String path, String search) again and again till the particular node file is reached. Now if it is a node file then it will search for the String or the given regular expression.

Let’s see a simple self explanatory java code. No need to add any jar file, directly you can run below the below program and see the output. Just provide a root path and String or valid regular expression to search.

/* SearchFile.java */

import java.io.File;

public class SearchFile {

 /* path : provide root directory from where it will search recursively.
  * search : String or valid regular expression. 
  */ 
 public void searchFile(String path,String search) {
  
  File file = new File(path);
  
  //System.out.println("Searching.."+file.getAbsolutePath());
  
  if(file.isDirectory()) {
   File[] allFilesInDirectory = file.listFiles();
   
   for (File eachFile : allFilesInDirectory) {  
    searchFile(eachFile.getAbsolutePath(),search);
   }
  }
  else {
   if(file.getName().matches(search)) {
    System.out.println("File Found >> "+file.getAbsolutePath());
   }
  }
 }
 public static void main(String[] args) {
  
  SearchFile serchObj = new SearchFile();
  //It will search for a jar file in C drive
  serchObj.searchFile("C:/",".*jar");
 }

}

Friday, November 11, 2011

Java : How to Run a Jar file

If you need to run a JAR file in JAVA, use below mentioned command

java -jar Test.jar

Note : In this case Manifest.txt inTest.jar should contain Entry Point i.e. Class which has main method in it.

Main-Class: MyPackage.MyClass

In some cases if there are multiple Entry points i.e. there is not any specific Main-Class in Manifest and you need to provide main class at run time then you can use below mentioned simple method.

java -cp Test.jar MyPackage.MyClass1
 
java -cp Test.jar MyPackage.MyClass2


Saturday, January 24, 2009

JSP : Simple File Uploading Codes.

We will see how to upload a file using JSP, you can upload a file to your desire location with desire name. Please see the simple self explanatory codes below.

Please add below mentioned jar files in your applications classpath.





1st HTML interface (can be used in JSP as well)

FileUploadingInterface.HTML


<HTML>
<HEAD>
<TITLE>File Uploading Interface</TITLE>
</HEAD>
<BODY>
<center>
<h3>Simple File Uploading Example</h3>
<form action="ProcessUploadedFile.jsp" method="post" enctype="multipart/form-data">
Name <input type="text" name="userName"> (optional) </br></br>
File <input type="file" name="file1"> </br></br> <!-- Here You Can Add more numbers of Files For Uploading -->
<input type="submit" value="Submit">
</form>
</center>
</BODY>
</HTML>



2nd JSP code


ProcessUploadedFile.jsp


<%@ page import="java.io.*" %>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%
boolean isMultipart = ServletFileUpload.isMultipartContent(request);

if (!isMultipart)
{
}
else
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try
{
items = upload.parseRequest(request);
}
catch (FileUploadException e)
{
e.printStackTrace();
}

Iterator itr = items.iterator();

while (itr.hasNext())
{
FileItem item = (FileItem) itr.next();

//Get Other Form Fileds Values in this Block
if (item.isFormField())
{
String itemName = item.getFieldName();
String itemValue = item.getString();
String name = "";
if (itemName.equals("userName"))
name = item.getString();

out.println("User Name "+name);
}
//Get File type Values Here, it could be one or multiple File
else
{
try
{
String itemName = item.getName();

//userFieldName is what u provided in html file
String userFieldName = item.getFieldName();

if(itemName != null)
{
if(itemName.equals(""))
{
//If Nothing Uploaded
out.println("No File Uploaded");
}
else
{
//Provide Destination Folder, Where Uploaded File to be saved
boolean folderMade = (new File("d:/UploadedFileFolder")).mkdirs();

// This is required to filter out file name from complete file path.
int idx = itemName.lastIndexOf("\\");

String filename = "";

if(idx > -1)
{
//in case of IE, it sends complete path. Hence only file name must be filtered out.
idx=idx+1;
filename = itemName.substring(idx);
}
else
{
//in case of other borwsers, its just filename
filename = itemName;
}

File savedFile = new File("d:/UploadedFileFolder/"+filename);
item.write(savedFile);

//Get path of the saved file
String path = savedFile.getPath();

out.println("File Uploaded Sucessfully "+path);

}
}
}//try
catch(Exception e)
{
e.printStackTrace();
}
}//else
}//while
}//else
%>