Sometimes a pure java implementation doesn't quite cut it. If you need to run other applications from within your app (you won't be able to do this from an applet), then here's what you need to do.
Step One
Obtain a java.lang.Runtime instance. The java.lang.Runtime class allows you to execute other applications, but you'll need an object reference before you can begin.
// Get runtime instance
Runtime r = Runtime.getRuntime();
Step Two
Next, you'll make a call to execute your application. The exec call returns a Process object, which gives you some control over the new process. If you just need to start something running, then you might want to discard the returned process.
// Exec my program
Process p = r.exec ("c:\\myprog\\inc");
Step Three
Now, if you want to do something with your process (perhaps kill it after a certain time period, or wait until its finished), you can use the methods of the java.lang.Process class. Check the Java API documentation for more information - there's plenty of things you can do. Suppose you wanted to wait until its terminated, and then continue with your app. Here's how you'd do it.
// Wait till its finished
p.waitFor();
SOURCE : www.referjava.com
Saturday, February 10, 2007
How do I playback multimedia files? I'd like to display .avi, .mpg, and other movie formats.
It'd be a tough task to write routines to playback a wide range of multimedia formats. Then you'd have to keep an eye out for new formats, write new code, and make it all so efficient that it can do real-time playback under a Java Virtual Machine. Fortunately, you don't have to do any of this!
Sun has been working on the Java Media Framework API, which offers an easy API to playback multimedia files. The API has been available for some time now, and implementations are now available from Sun, Intel, and others.
SOURCE : www.referjava.com
Sun has been working on the Java Media Framework API, which offers an easy API to playback multimedia files. The API has been available for some time now, and implementations are now available from Sun, Intel, and others.
SOURCE : www.referjava.com
How do I create a .JAR file?
Java Archive (JAR) files allow developers to package many classes into a single file. JAR files also use compression, so this can make applets and applications smaller.
Creating a .JAR file is easy. Simple go to the directory your classes are stored in and type :-
jar -cf myfile.jar *.class
If your application or applet uses packages, then you'll need to do things a little differently. Suppose your classes were in the package mycode.games.CoolGame - you'd change to the directory above mycode and type the following :- (Remember to use / on UNIX systems)
jar -cf myfile.jar .\mycode\games\CoolGame\*.class
Now, if you have an existing JAR file, and want to extract it, you'd type the following
jar -xf myfile.jar
SOURCE : www.referjava.com
Creating a .JAR file is easy. Simple go to the directory your classes are stored in and type :-
jar -cf myfile.jar *.class
If your application or applet uses packages, then you'll need to do things a little differently. Suppose your classes were in the package mycode.games.CoolGame - you'd change to the directory above mycode and type the following :- (Remember to use / on UNIX systems)
jar -cf myfile.jar .\mycode\games\CoolGame\*.class
Now, if you have an existing JAR file, and want to extract it, you'd type the following
jar -xf myfile.jar
SOURCE : www.referjava.com
What causes out of memory exceptions?
The dreaded OutOfMemoryException is something that no Java programmer ever wishes to see. Yet it can happen, particularly if your application involves a large amount of data processing, or is long lived - such as a server that responds to requests and is expected to run indefinitely.
There are several reasons that out of memory exceptions can be thrown. Either the virtual machine your Java program is running on may have a limit to the amount of memory it will give access to, the system on which your program is running may have run out of physical and virtual memory, or your application may just be consuming too much memory. In the first case, you may be able to modify the maximum heap size of the virtual machine, in the second or third you'll need to redesign your application.
Designing applications for minimal memory consumption isn't an easy task, and isn't specific only to Java programming. There are many techniques available, such as buffering data to disk that isn't needed at the moment, to using more efficient algorithms or subdividing tasks into smaller pieces. This is more of a design problem that a Java one - however there are some steps you can take to avoid your program crashing when it runs out of memory.
For a start, in your main method (or those you identify as likely to cause problems), catch OutOfMemoryExceptions. This way your application can terminate gracefully. Also, be sure to clear any data that you know you'll no longer require. The automatic garbage collector looks for objects that contain no references, so be sure to set your objects to null when you're finished. Finally, you can always examine the amount of free memory by calling the freeMemory() method of the system Runtime object. Here's how :
Runtime runtime = Runtime.getRuntime();
System.out.println ("Free memory : " - + runtime.freeMemory() );
If you realize that you're going to be performing large amounts of data processing (and consuming large amounts of memory), it might be a good idea to check before beginning. Here's to no more OutOfMemoryExceptions!
SOURCE : www.referjava.com
There are several reasons that out of memory exceptions can be thrown. Either the virtual machine your Java program is running on may have a limit to the amount of memory it will give access to, the system on which your program is running may have run out of physical and virtual memory, or your application may just be consuming too much memory. In the first case, you may be able to modify the maximum heap size of the virtual machine, in the second or third you'll need to redesign your application.
Designing applications for minimal memory consumption isn't an easy task, and isn't specific only to Java programming. There are many techniques available, such as buffering data to disk that isn't needed at the moment, to using more efficient algorithms or subdividing tasks into smaller pieces. This is more of a design problem that a Java one - however there are some steps you can take to avoid your program crashing when it runs out of memory.
For a start, in your main method (or those you identify as likely to cause problems), catch OutOfMemoryExceptions. This way your application can terminate gracefully. Also, be sure to clear any data that you know you'll no longer require. The automatic garbage collector looks for objects that contain no references, so be sure to set your objects to null when you're finished. Finally, you can always examine the amount of free memory by calling the freeMemory() method of the system Runtime object. Here's how :
Runtime runtime = Runtime.getRuntime();
System.out.println ("Free memory : " - + runtime.freeMemory() );
If you realize that you're going to be performing large amounts of data processing (and consuming large amounts of memory), it might be a good idea to check before beginning. Here's to no more OutOfMemoryExceptions!
SOURCE : www.referjava.com
How do I convert from a string into a number?
The first thing you have to be sure of is that your string actually contains a number! If not, then you'll generate a NumberFormatException. We can use the Integer class for converting from a string into an integer (or if we're dealing with decimal numbers, we can use the Double class).
The follow example shows how to convert from a string to an integer.
String numberOne = "123";
String numberTwo = "456";
// Convert numberOne and numberTwo to an int with Integer class
Integer myint1 = Integer.valueOf(numberOne);
Integer myint2 = Integer.valueOf(numberTwo);
// Add two numbers together
int number = myint1.intValue() + myint2.intValue();
You'll notice that when we need to use numbers as a data type (int), we need to convert from an Integer to an int. Confused? An Integer acts as a wrapper for our int data type (making conversion from String to number easy). When we want to use the data type, we call the intValue() method, which returns an int.
SOURCE : www.referjava.com
The follow example shows how to convert from a string to an integer.
String numberOne = "123";
String numberTwo = "456";
// Convert numberOne and numberTwo to an int with Integer class
Integer myint1 = Integer.valueOf(numberOne);
Integer myint2 = Integer.valueOf(numberTwo);
// Add two numbers together
int number = myint1.intValue() + myint2.intValue();
You'll notice that when we need to use numbers as a data type (int), we need to convert from an Integer to an int. Confused? An Integer acts as a wrapper for our int data type (making conversion from String to number easy). When we want to use the data type, we call the intValue() method, which returns an int.
SOURCE : www.referjava.com
How do I convert from an int to a char?
If you aren't interested in converting from an int to a string (from int 127 to string "127"), and only want to convert to an ASCII value, then you only need to cast from an int to a char.
What's casting? Casting is when we explicitly convert from one primitve data type, or a class, to another. Here's a brief example.
public class int_to_char
{
public static void main(String args[])
{
int a = 65;
char myChar = (char) a; // cast from int to char
System.out.println ("Char - " + myChar);
}
}
In this example, we have a number (65), which represents ASCII character A. We cast the number from an integer to a char, converting it to the letter A.
SOURCE : www.referjava.com
What's casting? Casting is when we explicitly convert from one primitve data type, or a class, to another. Here's a brief example.
public class int_to_char
{
public static void main(String args[])
{
int a = 65;
char myChar = (char) a; // cast from int to char
System.out.println ("Char - " + myChar);
}
}
In this example, we have a number (65), which represents ASCII character A. We cast the number from an integer to a char, converting it to the letter A.
SOURCE : www.referjava.com
How do I create a scrollable container?
The good news is there is already a scrollable container as part of the java.awt package. It's called ScrollPane, and without this, you'd need to create one from scratch, using scrollbars. Phew!
Its easy to create an instance of ScrollPane, and to then add it to your applet or application. Simply create a new instance of ScrollPane, and then add it to your applet's canvas. Whatever size your applet is, the scroll pane will adjust to. Now as you add components, scroll bars will appear as needed. You can also change the size of the scrollbar via the setSize() method. Here's a quick example
// Create a scroll pane object
ScrollPane myContainer = new ScrollPane();
// Add scroll pane to my current canvas (if I am an applet)
add(myContainer);
// Now I can add components to my container......
SOURCE : www.referjava.com
Its easy to create an instance of ScrollPane, and to then add it to your applet or application. Simply create a new instance of ScrollPane, and then add it to your applet's canvas. Whatever size your applet is, the scroll pane will adjust to. Now as you add components, scroll bars will appear as needed. You can also change the size of the scrollbar via the setSize() method. Here's a quick example
// Create a scroll pane object
ScrollPane myContainer = new ScrollPane();
// Add scroll pane to my current canvas (if I am an applet)
add(myContainer);
// Now I can add components to my container......
SOURCE : www.referjava.com
Subscribe to:
Posts (Atom)