Click to See Complete Forum and Search --> : Run other commands or applications..
Excelsoft
Jan 2nd, 2004, 08:13 AM
Hi there..
I've a question on Java; how can I run a command from the Java code, for example, I want when click on a JButton to send command to the system such as "dir" or "copy" or run applications by something like "start winword"..
plz help me it's so important for me..
I've another question: is there a component I can use to show an HTML page inside it on a frame, how it's used. I use JBuilder 5.
Thanx2all.
Dillinger4
Jan 2nd, 2004, 11:43 AM
As for your first question as far as i remember to run an application System.exec() was used but im not too sure though.
To display an html page you can use a javax.swing.JEditorPane which is able to handle frames, forms, hyperlinks, and parts of css Level 1. When you create your JEditorPane you can either pass in the actual html in the constructor with the mime type or you can just set the initial page with a Url passsed in as an arguement.
As i said before, JEditorPane is a able to handle hyperlinks. This is achieved with the use of HyperlinkListener interface or more specifically javax.swing.event.HyperlinkListener which contains this one method that you must implement. public void hyperlinkUpdate(HyperlinkEvent e)
Excelsoft
Jan 3rd, 2004, 11:38 AM
thanx 4 ur reply, I'll check that then tell u.
thanx again.
CornedBee
Jan 7th, 2004, 04:28 AM
I should add some things.
For the copy and dir commands, you'd best do that in code via the java.io.File class.
Executing applications is done with System.exec (or maybe it was Runtime.getRuntime().exec).
However, you cannot "execute" data files, like word documents. I wrote a small JNI class to do that, but I still need Linux and MacOS implementations.
Excelsoft
Jan 7th, 2004, 11:01 AM
thanx CornBee 4 ur reply, but I'm beginner in Java may u please write an example of how these commands are to be written.
CornedBee
Jan 7th, 2004, 11:40 AM
Ok, there's no copy command. Strange...
To list the files of a directory, use File.list or File.listFiles.
import java.io.File;
public class Dir {
public static void main(String[] args) {
File cwd = new File(".").getAbsolutePath();
System.out.println("Index of " + cwd);
System.out.println();
String[] files = cwd.list();
for(int i = 0; i < files.length; ++i) {
System.out.println(files[i]);
}
}
}
Usage:
java Dir
You can execute any executable using Runtime.exec().
public class Start {
public static void main(String[] args) throws Exception {
if(args.length < 1) {
System.out.println("You must specify a file.");
return;
}
Runtime.getRuntime().exec(args[0]);
}
}
Usage:
java Start <executable name>
Excelsoft
Jan 8th, 2004, 05:12 AM
thanx again I'll try this and tell you..
Excelsoft
Jan 13th, 2004, 06:17 AM
Thanx , it worked!!
MystBlue
Feb 5th, 2004, 12:00 PM
umm.. is it possible to code where java is able to click and parse information(such as password and username) to the third party program that was run...
example:
java runs my msn messenger and press the sign in button
it then fills the username and password and press Ok button for me...
I hope something like this is possible. If it is, pls guide me. Thank you.
CornedBee
Feb 5th, 2004, 05:39 PM
You can look at the java.awt.Robot class, but it's some unpleasant stuff to do.
Which is why good applications should always provide a command line interface.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.