Java Examples - Set multiple classpath
Problem Description:
How to set multiple classpath?
Solution:
Following example demonstrates how to set multiple classpath. Multiple class paths are separated by a semicolon.
c:> java -classpath C:\java\MyClasse1;C:\java\MyClass2
utility.testapp.main
Result:
The above code sample will produce the following result.
Class path set.
Java Examples - Debug a java file
Problem Description:
How to set destination of the class file?
Solution:
Following example demonstrates how to debug a java file using =g option with javac command.
c:> javac demo.java -g
Result:
The above code sample will produce the following result.
Demo.java will debug.
Java Examples - Set classpath
Problem Description:
How to set classpath?
Solution:
Following example demonstrates how to set classpath.
C:> java -classpath C:\java\DemoClasses utility.demoapp.main
Result:
The above code sample will produce the following result.
Class path set.
Java Examples - View classpath?
Problem Description:
How to view current classpath?
Solution:
Following example demonstrates how to view current classpath using echo command.
C:> echo %CLASSPATH%
Result:
The above code sample will produce the following result.
.;C:\Program Files\Java\jre1.6.0_03\lib\ext\QTJava.zip
Java Examples - Destination of class file
Problem Description:
How to set destination of the class file?
Solution:
Following example demonstrates how to set destination of the class file that will be created after compiling a java file using -d option with javac command.
c:> javac demo.java -d c:\myclasses
Result:
The above code sample will produce the following result.
Demo application executed.
Java Examples - Run a class file
Problem Description:
How to run a class file?
Solution:
Following example demonstrates how to run a class file from command prompt using java command.
c:\jdk\demoapp>java First
Result:
The above code sample will produce the following result.
Demo application executed.
Java Examples - Version of Java
Problem Description:
How to check version of java running on your system?
Solution:
Following example demonstrates how to check version of java installed on your system using version argument with java command.
java -version
Result:
The above code sample will produce the following result.
java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)
Java Examples - Set classpath
Problem Description:
How to set classpath when class files are in .jar file?
Solution:
Following example demonstrates how to set class path when classes are stored in a .jar or .zip file.
c:> java -classpath C:\java\myclasses.jar utility.testapp.main
Result:
The above code sample will produce the following result.
Class path set.
Java Examples - Comparing two strings
Problem Description:
How to compare two strings ?
Solution:
Following example compares two strings by using str compareTo (string) , str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ascii difference of first odd characters of compared strings .
public class StringCompareEmp{
public static void main(String args[]){
String str = "Hello World";
String anotherString = "hello world";
Object objStr = str;
System.out.println( str.compareTo(anotherString) );
System.out.println( str.compareToIgnoreCase(anotherString) );
System.out.println( str.compareTo(objStr.toString()));
}
}
Result:
The above code sample will produce the following result.
-32
0
0
Java Examples - Searching last occurance ?
Problem Description:
How to search the last position of a substring ?
Solution:
This example shows how to determine the last position of a substring inside a string with the help of strOrig.lastIndexOf(Stringname) method.
public class SearchlastString {
public static void main(String[] args) {
String strOrig = "Hello world ,Hello Reader";
int lastIndex = strOrig.lastIndexOf("Hello");
if(lastIndex == - 1){
System.out.println("Hello not found");
}else{
System.out.println("Last occurrence of Hello
is at index "+ lastIndex);
}
}
}
Result:
The above code sample will produce the following result.
Last occurrence of Hello is at index 13
Java Examples - Deleting a character
Problem Description:
How to remove a particular character from a string ?
Solution:
Following example shows hoe to remove a character from a particular position from a string with the help of removeCharAt(string,position) method.
public class Main {
public static void main(String args[]) {
String str = "this is Java";
System.out.println(removeCharAt(str, 3));
}
public static String removeCharAt(String s, int pos) {
return s.substring(0, pos) + s.substring(pos + 1);
}
}
Result:
The above code sample will produce the following result.
thi is Java
Java Examples - Replacing a String
Problem Description:
How to replace a substring inside a string by another one ?
Solution:
This example describes how replace method of java String class can be used to replace character or substring by new one.
public class StringReplaceEmp{
public static void main(String args[]){
String str="Hello World";
System.out.println( str.replace( 'H','W' ) );
System.out.println( str.replaceFirst("He", "Wa") );
System.out.println( str.replaceAll("He", "Ha") );
}
}
Result:
The above code sample will produce the following result.
Wello World
Wallo World
Hallo World
No comments:
Post a Comment