Java Forum / First Aid / December 2005
Writing a application using "process"
bherbst65@hotmail.com - 09 Dec 2005 06:34 GMT Hi All,
In another programming language, I can use "processes" to call IE and a specific html address .
unit GetEE_JavaViaIEUnit1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, FileCtrl;
type TForm1 = class(TForm) RadioGroup1: TRadioGroup; procedure RadioGroup1Click(Sender: TObject); private procedure Cprocess(sExecuteFile: string; Sender: Tobject); { Private declarations } public
{ Public declarations } end;
var Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Cprocess(sExecuteFile: string; Sender: TObject); var pi: TProcessInformation; si: TStartupInfo; begin FillMemory(@si, sizeof(si), 0); si.cb := sizeof(si); CreateProcess(nil, PChar(sExecuteFile), nil, nil, false, NORMAL_PRIORITY_CLASS, nil, nil, si, pi); // close up shop... closeHandle(pi.hProcess); closeHandle(pi.hThread); end;
procedure TForm1.RadioGroup1Click(Sender: TObject); var Str1, Str2, Totalcmdline: string; begin Str1 := 'C:\Program Files\Internet Explorer\IEXPLORE.EXE'; if RadioGroup1.ItemIndex = 0 then begin Str2 := 'http://oldlook.experts-exchange.com/Programming/Programming_Languages/Java/'; end; if RadioGroup1.ItemIndex = 1 then begin Str2 := 'http://www.experts-exchange.com/Programming/Programming_Languages/Java/'; end; Totalcmdline := str1 + ' "' + str2 + ' "'; Cprocess(Totalcmdline, Self); end; end.
Is it possible using the term "process" to write this above in Java language? a demo would be appreciated.
I do have this program with the term "process" to reset my computer's time getting the correct time from NIST. Here it is:
import java.net.*; import java.io.*; import java.util.*;
/** * <p>Title: Reset Computer's Time Using Java</p> * @version 1.0 */ // Message from NIST appears as String // 51863 00-11-15 00:50:05 00 0 0 938.7 UTC(NIST) *
public class resetComputersTimeUsingJava {
public static void main(String[] args) throws Exception { int tl, t2, TimeZone, HourAdjusted; String inputTimeLine, DateStatement, TimeStatement, HourAdjustedStr; inputTimeLine = " "; HourAdjustedStr = " "; // with socket address
java.net.Socket s = new java.net.Socket("132.163.4.102", 13);
// in May 2005: URL NIST = new URL("http:// updated url "132.163.4.102:13"); in 2005
// URLConnection NISTConnection = NIST.openConnection(); // BufferedReader in = new BufferedReader( // new InputStreamReader(NISTConnection.getInputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); DateStatement = " "; TimeStatement = " "; while ((inputTimeLine = in.readLine())!= null) { System.out.println( inputTimeLine ); System.out.println( inputTimeLine.length() ); tl = inputTimeLine.length(); if (tl > 5 ) // one of the input forms came in with a blank line before DateTime
{ TimeZone = 4 ; // Eastern Time // not sure of this ???? HourAdjusted = Integer.parseInt(inputTimeLine.substring(15,17))- TimeZone; if (HourAdjusted < 0){ switch (HourAdjusted) { // incomplete... for hour only error does NOT include day error... case -7: HourAdjusted = 17; break; // Pacific Time case -6: HourAdjusted = 18; break; // Mountain Time case -5: HourAdjusted = 19; break; // Central Time case -4: HourAdjusted = 20; break; // Eastern Time case -3: HourAdjusted = 21; break; // Eastern Time - 1 case -2: HourAdjusted = 22; break; // Eastern Time - 2 case -1: HourAdjusted = 23; break; // Eastern Time - 3 } }
HourAdjustedStr = Integer.toString(HourAdjusted); DateStatement = "date " + inputTimeLine.substring(9, 11) + "/" + inputTimeLine.substring(12, 14) + "/" + "20" + inputTimeLine.substring(6, 8) ; TimeStatement = "time " + HourAdjustedStr + ":" + inputTimeLine.substring(18,20) + ":"+ inputTimeLine.substring(21,23); System.out.println(DateStatement); System.out.println(TimeStatement); Date today = new Date();
try { Process p3 = Runtime.getRuntime().exec("cmd /c " + DateStatement); InputStream inDate = p3.getInputStream(); int r3; while((r3 = inDate.read()) != -1) { p3.waitFor(); System.out.write(r3); } } catch (Exception e2) { System.out.println("Here DateStatement Exception"); }
try { Process p4 = Runtime.getRuntime().exec("cmd /c " + TimeStatement); InputStream inDate = p4.getInputStream(); int r4; while((r4 = inDate.read()) != -1) { p4.waitFor(); System.out.write(r4); } } catch (Exception e3) { System.out.println("Here TimeStatement Exception"); } } } } }
Bob
Roedy Green - 09 Dec 2005 06:58 GMT >In another programming language, I can use "processes" to call IE and >a specific html address . see http://mindprod.com/jgloss/exec.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
bherbst65@hotmail.com - 11 Dec 2005 18:02 GMT Hi Roedy, Thanks for you help. I appreciate it.
Here below is the program that was shown above in another language, now translated to Java. Very slick how java 'seems'? to wait until IE is loaded before inserting the additional line. This method is a lot easier to use than an applet or fighting with a troublesome url as noted in another recent Google post of:
http://groups.google.com/group/comp.lang.java.help/browse_frm/thread/743e63889a6 881d1/e
a2f94287c074909?hl=en#ea2f94287c074909 and the difficulty the author was having to show the url : "http://www.google.com/search?hl=en&q=help+me&btnG=Google+Search"
Thanks again, Bob
import java.awt.*; import java.awt.event.*; import javax.swing.*;
/* A runtime program to get IE * and then a specific htrml address * is placed into IE to complete the lookup. * used Win2k with NT and jdk1.5.0_05 */
public class RuntimeIEBttns extends JPanel implements ActionListener { public JButton bOL, bNL, bCL,bGG;
public RuntimeIEBttns() {
bOL = new JButton("Old Look at EE"); bOL.setActionCommand("oldlook");
bNL = new JButton("New Look at EE"); bNL .setActionCommand("newlook");
bGG = new JButton("Google Help Me"); bGG .setActionCommand("gogoogle");
bCL = new JButton("Close Application"); bCL.setActionCommand("close");
//Listen for actions on buttons bOL , bNL and bCL. bOL.addActionListener(this); bNL.addActionListener(this); bCL.addActionListener(this); bGG.addActionListener(this);
//Add Components to this container, using the default FlowLayout. add(bOL); add(bNL); add(bGG); add(bCL); }
public void actionPerformed(ActionEvent e) { String Str1 = "C:/Program Files/Internet Explorer/IEXPLORE.EXE"; if (e.getActionCommand().equals("oldlook")) { try{ String Str2
="http://oldlook.experts-exchange.com/Programming/Programming_Languages/Java/";
Runtime rt = Runtime.getRuntime(); rt.exec(new String[] {Str1,Str2}); } catch (Exception e3) {System.out.println("ERR");} }
if (e.getActionCommand().equals("newlook")) { try{ String Str3
="http://www.experts-exchange.com/Programming/Programming_Languages/Java/";
Runtime rt = Runtime.getRuntime(); rt.exec(new String[] {Str1,Str3}); } catch (Exception e3) {System.out.println("ERR");} }
if (e.getActionCommand().equals("gogoogle")) { try{ String Str4 ="http://www.google.com/search?hl=en&q=help+me&btnG=Google+Search";
Runtime rt = Runtime.getRuntime(); rt.exec(new String[] {Str1,Str4}); } catch (Exception e3) {System.out.println("ERR");} }
if (e.getActionCommand().equals("close")) {System.exit(0);} }
public static void main(String[] args) { JFrame frame = new JFrame("Runtime Buttons Demo"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
frame.getContentPane().add(new RuntimeIEBttns(), BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } }
Roedy Green - 11 Dec 2005 20:18 GMT > String Str4 >="http://www.google.com/search?hl=en&q=help+me&btnG=Google+Search"; this is a bad name. First it starts with a capital letter. variables should start with lower case. Next is it meaningless. Call it "googleSearch"
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 11 Dec 2005 20:24 GMT >Runtime rt = Runtime.getRuntime(); > rt.exec(new String[] {Str1,Str4}); > } > catch (Exception e3) {System.out.println("ERR");} all this could be factored:
private void showWithIE( String url ) { try { Runtime.getRuntime().exec(new String[] {INVOKE_IE, ,url}); catch ( Exeception e ) { System.out.println("IE failed to render " + url + " " + e.getMessage() ); } }
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
bherbst65@hotmail.com - 12 Dec 2005 02:41 GMT Roedy, I am puzzled why you changed my title of the Q to what it is now.
Looking at your help immediately above, there are a few more items:
1. Why go to this trouble println System.out.println("IE failed to render " when the error will come after mounting IE? I tried to make it happen with a incorrect html and the error is reported at the browser level not at the level of the actual URL.
2 For the error that you report with my work you mention that variables should start with lower case, I have made the changes and renamed them accordingly. And I have edited the program to reflect your suggestions about screening the URL for correctness, however, there is an error in your gift spelling Exception which I have corrected. ( Runtime.getRuntime().exec(new String[] {INVOKE_IE, ,url}); catch ( Exeception e ) { Further, if an error did happen with the url it would be reported on the IE page, not in the java program. I tried to error the url and it was reported on the IE page and not in the java program.
Bob
Roedy Green - 12 Dec 2005 03:46 GMT > ( Runtime.getRuntime().exec(new String[] {INVOKE_IE, ,url}); > catch ( Exeception e ) > { the more serious error is the double comma.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 12 Dec 2005 03:48 GMT >Further, if an error did happen with the url it would be reported on >the IE page, not in the java program. True, if all works according to plan. But if all works according to plan no exceptions will be thrown. In real life it often does not. That is why I dumped out all the relevant details. The wording is misleading. You are right it should say something like "failed to launch" rather than "failed to render"
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 12 Dec 2005 03:49 GMT >I am puzzled why you changed my title of the Q to what it is now. Have you ever seen the movie "The Gods Must Be Crazy". A man manages through misadventure to winch his Land Rover high into a tree. His sidekick says, "Why did you do that, Boss?".
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
bherbst65@hotmail.com - 12 Dec 2005 14:14 GMT Hi Roedy, While you are on the line about my runtime program, here is a bit of the program rewritten: I still have a couple of questions. ...................... public void actionPerformed(ActionEvent e) { String urlEEOldlook
="http://oldlook.experts-exchange.com//Programming_Languages/Java/"; String urlEENewLook =
"http://www.experts-exchange.com/Programming/Programming_Languages/Java/"; String urlGoogleSearch
="http://www.google.com/search?hl=en&q=help+me&btnG=Google+Search"; if (e.getActionCommand().equals("oldLookAtEE")) { showWithIE (urlEEOldlook ) ;} else if (e.getActionCommand().equals("newLookAtEE")) { showWithIE (urlEENewLook ) ;} else if (e.getActionCommand().equals("helpmeGoogleSearch")) { showWithIE (urlGoogleSearch) ;} else if (e.getActionCommand().equals("close")) {System.exit(0);} }
private void showWithIE( String url ){ String INVOKE_IE = "C:/Program Files/Internet Explorer/IEXPLORE.EXE"; { try { Runtime.getRuntime().exec(new String[] {INVOKE_IE, url}); } catch ( Exception e ) { System.out.println("IE failed to launch url " + url + " " + e.getMessage() ); } } } .......................
Question:
1. Is the "else if" arrangement ok?
2. We were on the issue of variables and writing style..... what does this mean: "INVOKE_IE" ? Is it a string or not? If it is a string then "invoke_IE"? If not then please explain.
Bob
Roedy Green - 12 Dec 2005 22:15 GMT >1. Is the "else if" arrangement ok? It looks syntactically correct. The compiler is better at matching {} than my eyes are.
I prefer the new line style of brace, but that's something you can globally configure in Eclipse to flip back and forth.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 12 Dec 2005 22:18 GMT >2. We were on the issue of variables and writing style..... what does >this mean: >"INVOKE_IE" ? see http://mindprod.com/jgloss/codingconventions.html
private static final String INVOKE_IE is a constant, hence all caps.
you moved that back to being an ordinary String variable.
I find it better to collect together constants in one place where they are easy to find rather than dotted in the code. You might as well use the String literal the way you did it.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
bherbst65@hotmail.com - 13 Dec 2005 00:27 GMT Hi Roedy,
I will use this program that is well formed as a model for further modifications.
For starters, enhancements will probably include imageicons on the buttons. URL picURL = getClass().getResource("Bird.gif"); ImageIcon icon = new ImageIcon(picURL);
I didn't include it here as it would only clutter the main thought.
You wrote:
>It looks syntactically correct. The compiler is better at >matching {} than my eyes are. For me, it is just great to use editor that has a connecting colored band to identify the start to the end of a matching pair of { }'s . Thanks very much,
Bob
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|