Monday, August 17, 2009

Eclipse build error : Cannot find the class file for java.lang.Object.

The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project

Seems the default JRE is not being set properly to the project that is showing this error.
All you need to do is check the JRE System Library.

Go to the project ->properties->java build path->libraries

Add Library- JRE SYSTEM LIBRARY ->Next
and now choose from the installed jre's

Alternatively you could also add the following to your classpath file :

path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.5.0_15"/>

That should solve the problem.

Monday, July 27, 2009

Open a new browser from a standalone java program

import java.io.IOException;

public class OpenBrowser
{
public static void displayURL(String url)
{
boolean windows = isWindowsPlatform();
String cmd = null;
try
{
if (windows)
{
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
Process p = Runtime.getRuntime().exec(cmd);
}
else
{
// Under Unix, Netscape has to be running for the "-remote"
// command to work. So, we try sending the command and
// check for an exit value. If the exit command is 0,
// it worked, otherwise we need to start the browser.
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
cmd = UNIX_PATH + " " + url;
p = Runtime.getRuntime().exec(cmd);
}
}
catch(InterruptedException x)
{
System.err.println("Error bringing up browser, cmd='" +
cmd + "'");
System.err.println("Caught: " + x);
}
}
}
catch(IOException x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);
}
}
/**
* Try to determine whether this application is running under Windows
* or some other platform by examing the "os.name" property.
*
* @return true if this application is running under a Windows OS
*/
public static boolean isWindowsPlatform()
{
String os = "os.name";
if ( os != null && os.startsWith(WIN_ID))
return true;
else
return false;
}
/**
* Simple example.
*/
public static void main(String[] args)
{
displayURL("http://www.google.com");
}
// Used to identify the windows platform.
private static final String WIN_ID = "Windows";
// The default system browser under windows.
private static final String WIN_PATH = "rundll32";
// The flag to display a url.
private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
// The default browser under unix.
private static final String UNIX_PATH = "netscape";
// The flag to display a url.
private static final String UNIX_FLAG = "-remote openURL";

}

Courtesy : here

Wednesday, March 25, 2009

Image compressor

Found this piece of code on www.coderanch.com

package com;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class ImageCompression
{
public static void main(String[] args) throws IOException
{
Iterator writers;
BufferedImage bufferedImage;
ImageOutputStream imageOutputStream;
ImageWriter imageWriter;
ImageWriteParam pngparams;
// Read an image from the disk (First Argument)
// bufferedImage = ImageIO.read(new File(args[0]));
bufferedImage = ImageIO.read(new File("C:\\DSC0009111370.JPG"));
// Get all the PNG writers
writers = ImageIO.getImageWritersByFormatName("jpg");
// Fetch the first writer in the list
imageWriter = (ImageWriter) writers.next();
// Just to confirm that the writer in use is CLibPNGImageWriter
System.out.println("\n Writer used : " + imageWriter.getClass().getName() + "\n");
// Specify the parameters according to those the output file will be
// written
// Get Default parameters
pngparams = imageWriter.getDefaultWriteParam();
// Define compression mode
pngparams.setCompressionMode(javax.imageio.ImageWriteParam.MODE_EXPLICIT);
// Define compression quality
pngparams.setCompressionQuality(0.5F);
// Define progressive mode
pngparams.setProgressiveMode(javax.imageio.ImageWriteParam.MODE_COPY_FROM_METADATA);
// Deine destination type - used the ColorModel and SampleModel of the
// Input Image
pngparams.setDestinationType(new ImageTypeSpecifier(bufferedImage.getColorModel(), bufferedImage
.getSampleModel()));
// Set the output stream to Second Argument
// imageOutputStream = ImageIO.createImageOutputStream( new
// FileOutputStream(args[1]) );
imageOutputStream = ImageIO.createImageOutputStream(new FileOutputStream("C:\\new_test.jpg"));
imageWriter.setOutput(imageOutputStream);
// Write the changed Image
imageWriter.write(null, new IIOImage(bufferedImage, null, null), pngparams);
// Close the streams
imageOutputStream.close();
imageWriter.dispose();
}
}

Tuesday, March 17, 2009

SPLIT A TEXT FILE TO A SPECIFIED NO. OF SUBFILES

package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class SplitFile
{
/*The method splitFile(File source,int noFile,int lsize) splits the file 'source' into 'noFile' no. of individual files, each file having 'lsize' no. of characters*/

public static File[] splitFile(File source,int noFile,int lsize)
{
File[] fileFragments = new File[noFile];
String[] frgfName = new String[noFile];

try{
String sourceFName = source.getName();
long sourceFSize = source.length();
FileInputStream fis = new FileInputStream(source);

System.out.println(noFile);
if (lsize != 0) {
noFile--;
}
System.out.println(noFile);
sourceFName = sourceFName.substring(0, sourceFName.lastIndexOf("."));
int j=0;
for (int i = 1; i <= noFile; i++) {
frgfName[i-1] = "D:\\"+sourceFName + String.valueOf(i)+".txt";
fileFragments[i-1] = new File(frgfName[i-1]);

FileOutputStream fos = new FileOutputStream(fileFragments[i - 1]);
byte[] data = new byte[lsize];
int count = fis.read(data);
fos.write(data);
fos.close();
String frgFileInfo = new String(frgfName[i-1] + "," + String.valueOf(lsize));
}
if (lsize != 0) {
System.out.println(noFile);
frgfName[noFile] = "D:\\"+sourceFName + String.valueOf(noFile+1)+".txt";
fileFragments[noFile] = new File(frgfName[noFile]);
FileOutputStream fos = new FileOutputStream(fileFragments[noFile]);
byte[] data = new byte[lsize];
int count = fis.read(data);
fos.write(data);
fos.close();
String frgFileInfo = new String(frgfName[noFile] + "," + String.valueOf(lsize));
}

} catch (Exception e) {
System.out.println("Error in Splitting:"+e);
return null;
}
return fileFragments;

}

public static void main(String[] args) {

File keyfile = new File("D:/test.txt"); // the path to the source file
File[] resultFiles=null;
resultFiles=splitFile(keyfile,5,30);
for(File fileObj:resultFiles){
System.out.println(fileObj.getAbsolutePath());
}

}
}

Wednesday, January 21, 2009

use of cascade in hbm file

1) cascade="none", the default, tells Hibernate to ignore the association.
2) cascade="save-update" tells Hibernate to navigate the association when transaction is committed and when an object is passed to save() or
update() and save newly instantiated transient instances and persist changes to
detached instances.
3) cascade=""delete" tells Hibernate to navigate the association and delete persistent
instances when an object is passed to delete().
4) cascade="all" means to cascade both save-update and delete, as well as
calls to evict and lock.
5) cascade="all-delete-orphan" means the same as cascade="all" but, in addition,
Hibernate
delete any persistent entity instance that has been removed
(dereferenced) from the association (for example, from a collection).
6) cascade="delete-orphan" Hibernate will delete any persistent
entity
instance that has been removed (dereferenced) from the association (for
example, from a collection).