You can write a runnable Java program which does not have main method at all. This can be done using the static block of the class.
The reason this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);
}
}
Monday, August 9, 2010
Wednesday, August 4, 2010
Awt, Swing and SWT
AWT: The idea was to wrap the native GUI widgets of the various operating systems with a platform-independent Java API called Abstract Window Toolkit (AWT). Only common widgets such as text field, text area, check box, radio button, list, and push button were supported by AWT. The graphics and imaging features were also very limited. That was, at best, enough for building simple applets.
SWING: is one of the most complex GUI frameworks ever developed. It has a complete set of GUI components ranging from buttons and text fields to tables, trees, and styled text editors. These components do not rely on the native widgets of the operating system; instead, Swing components are painted using graphic primitives such as lines, rectangles, and text. The painting is delegated to a look and feel (L&F) plug-in that can imitate the native L&F. Swing also has a platform-independent L&F called "Metal." JBuilder, uses Swing, and its speed is quite good.
Standard Widget Toolkit (SWT) is the GUI toolkit developed by IBM for its Eclipse IDE. SWT can be used outside of the Eclipse environment and offers direct access to the native GUI features of the operating system. Therefore, SWT-based Java applications have native GUIs and can be integrated with other native applications and components. SWT delegates to native widgets for common components (such as labels, lists, tables, and so on) as AWT does, while emulating in Java more sophisticated components (for example, toolbars are emulated when running on Motif) similarly to Swing's strategy.SWT has been designed to be as inexpensive as possible. This means (among the other things) that it is native-oriented. Anyway, it differs from AWT in a number of details. SWT provides different Java implementations for each platform, and each of these implementations calls natively (through the Java Native Interface, JNI) the underlying native implementation. The old AWT is different in that all platform-dependent details are hidden in C (native) code and the Java implementation is the same for all the platforms.
SWING: is one of the most complex GUI frameworks ever developed. It has a complete set of GUI components ranging from buttons and text fields to tables, trees, and styled text editors. These components do not rely on the native widgets of the operating system; instead, Swing components are painted using graphic primitives such as lines, rectangles, and text. The painting is delegated to a look and feel (L&F) plug-in that can imitate the native L&F. Swing also has a platform-independent L&F called "Metal." JBuilder, uses Swing, and its speed is quite good.
Standard Widget Toolkit (SWT) is the GUI toolkit developed by IBM for its Eclipse IDE. SWT can be used outside of the Eclipse environment and offers direct access to the native GUI features of the operating system. Therefore, SWT-based Java applications have native GUIs and can be integrated with other native applications and components. SWT delegates to native widgets for common components (such as labels, lists, tables, and so on) as AWT does, while emulating in Java more sophisticated components (for example, toolbars are emulated when running on Motif) similarly to Swing's strategy.SWT has been designed to be as inexpensive as possible. This means (among the other things) that it is native-oriented. Anyway, it differs from AWT in a number of details. SWT provides different Java implementations for each platform, and each of these implementations calls natively (through the Java Native Interface, JNI) the underlying native implementation. The old AWT is different in that all platform-dependent details are hidden in C (native) code and the Java implementation is the same for all the platforms.
Tuesday, August 3, 2010
java.lang » System Properties
System Properties
* Getting and Setting the Value of a System Property
* Listing All System Properties
* Setting the Value of a System Property from the Command Line
Getting and Setting the Value of a System Property
// Get a system property
String dir = System.getProperty("user.dir");
// Set a system property
String previousValue = System.setProperty("application.property", "newValue");
--------------------------------------------------------------------------------
Listing All System Properties
// Get all system properties
Properties props = System.getProperties();
// Enumerate all system properties
Enumeration enum = props.propertyNames();
for (; enum.hasMoreElements(); )
{
// Get property name
String propName = (String)enum.nextElement();
// Get property value
String propValue = (String)props.get(propName);
}
---------------------------------------------------------------------------------
Setting the Value of a System Property from the Command Line
A system property can be set or overridden by specifying the -D option to the java command when running your program.
java -Dmy.prop="my value" MyApp
// Get the value of the system property
String prop = System.getProperty("my.prop");
// my value
* Getting and Setting the Value of a System Property
* Listing All System Properties
* Setting the Value of a System Property from the Command Line
Getting and Setting the Value of a System Property
// Get a system property
String dir = System.getProperty("user.dir");
// Set a system property
String previousValue = System.setProperty("application.property", "newValue");
--------------------------------------------------------------------------------
Listing All System Properties
// Get all system properties
Properties props = System.getProperties();
// Enumerate all system properties
Enumeration enum = props.propertyNames();
for (; enum.hasMoreElements(); )
{
// Get property name
String propName = (String)enum.nextElement();
// Get property value
String propValue = (String)props.get(propName);
}
---------------------------------------------------------------------------------
Setting the Value of a System Property from the Command Line
A system property can be set or overridden by specifying the -D option to the java command when running your program.
java -Dmy.prop="my value" MyApp
// Get the value of the system property
String prop = System.getProperty("my.prop");
// my value
OS' environment variables from within a Java program
import java.util.Map;
public class EnvMap {
public static void main (String[] args) {
Map env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
}
}
public class EnvMap {
public static void main (String[] args) {
Map
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
}
}
Monday, August 2, 2010
Wednesday, July 28, 2010
Ant
> Ant is a pure Java build tool
> Ant allows the developer to automate the repeated process involved in the development of J2EE application.
> Developers can easily write the script to automate the build process like compilation, archiving and deployment.
> Downloading and Installing Ant
> Set the class path to the bin directory of the ant.
Let's assume that Ant is installed in c:\ant\. The following code has to be put into autoexec.bat file:
set ANT_HOME=c:\ant
set JAVA_HOME=c:\Program Files\Java\jdk1.6.0\
set PATH=%PATH%;%ANT_HOME%\bin
Testing Ant
Go to command prompt and issue the following command.
C:\anttest>Ant
Buildfile: build.xml does not exist!Build failed
C:\anttest>
If every this is installed correctly Ant will give the above message.
Now its time to do some work with Ant.
Ant uses configuration file called build.xml to work. This is the file where you defines the process of compiling, building and deploying.
Writing build.xml file
build.xml is a xml file used by ant utility to compile, build and deploy or run the application.
Now here is code of simple build.xml file which compiles a java file present in src directory and places compiled class file in build/src directory.
<?xml version="1.0"?>
<!-- Build file for our first application -->
< project name="Ant test project" default="build" basedir=".">
< target name="build" >
< javac srcdir="src" destdir="build/src" debug="true"
includes="**/*.java"
/>
</target>
</project>
-------------------------------------------------------------------------
The project tag:
requires three attributes namely name, default and basedir.
Here is the description of the attributes:
name |Represents the name of the project.
----------------------------------------------------
default |Name of the default target to use when no target is supplied.
basedir |Name of the base directory from which all path calculations are done.
All the attributes are required.
—-build1.xml ——–
< project name=”hello” default=”hello”>
< target name=”hello”>
< echo message=”Hello, World”/>
</target>
< target name=”goodbye”>
< echo message=”Goodbye, end of Hello world script”/>
< /target>
< /project>
> Ant allows the developer to automate the repeated process involved in the development of J2EE application.
> Developers can easily write the script to automate the build process like compilation, archiving and deployment.
> Downloading and Installing Ant
> Set the class path to the bin directory of the ant.
Let's assume that Ant is installed in c:\ant\. The following code has to be put into autoexec.bat file:
set ANT_HOME=c:\ant
set JAVA_HOME=c:\Program Files\Java\jdk1.6.0\
set PATH=%PATH%;%ANT_HOME%\bin
Testing Ant
Go to command prompt and issue the following command.
C:\anttest>Ant
Buildfile: build.xml does not exist!Build failed
C:\anttest>
If every this is installed correctly Ant will give the above message.
Now its time to do some work with Ant.
Ant uses configuration file called build.xml to work. This is the file where you defines the process of compiling, building and deploying.
Writing build.xml file
build.xml is a xml file used by ant utility to compile, build and deploy or run the application.
Now here is code of simple build.xml file which compiles a java file present in src directory and places compiled class file in build/src directory.
<?xml version="1.0"?>
<!-- Build file for our first application -->
< project name="Ant test project" default="build" basedir=".">
< target name="build" >
< javac srcdir="src" destdir="build/src" debug="true"
includes="**/*.java"
/>
</target>
</project>
-------------------------------------------------------------------------
The project tag:
requires three attributes namely name, default and basedir.
Here is the description of the attributes:
name |Represents the name of the project.
----------------------------------------------------
default |Name of the default target to use when no target is supplied.
basedir |Name of the base directory from which all path calculations are done.
All the attributes are required.
—-build1.xml ——–
< project name=”hello” default=”hello”>
< target name=”hello”>
< echo message=”Hello, World”/>
</target>
< target name=”goodbye”>
< echo message=”Goodbye, end of Hello world script”/>
< /target>
< /project>
Subscribe to:
Posts (Atom)