Results 1 to 10 of 10
  1. #1
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Question [SOLVED] Making a program to open a file (.exe)

    Ok i have been trying to create a java program that would open an executable file when entered a password. (I am making the java program an executable using exe4j)
    This is my code so far: (it does not work because you can't do: file.open())

    import java.util.Scanner;
    import java.io.*;

    public class Passcode
    {
    public static void main (String[]args)
    {
    Scanner keyB = new Scanner (System.in);

    String ans = "password";
    String pass;

    System.out.println("Enter a password: ");
    pass = keyB.nextLine();

    if (pass.equals(ans))
    {
    File file = new File("C:/Program Files/WarCraft3/WC3.EXE");
    file.open();

    }
    else
    System.out.println("Invalid Password");

    }
    }

    Does anybody have any suggestions?
    Please reply.
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Please read Runtime Class doc

  3. #3
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    Hi Linux,

    Hope this helps:

    The easiest way for you to run exe at run time (i.e., you'll have to make
    sure that it exists) is to use the Java's built-in Process.

    The general way is
    Process process = Runtime.getRuntime().exec(as, es, dir);

    where as is the String [] of the command (full-path) and its arguments,
    es is the String [] of the environments (more suitable in unix)
    dir is a File specifying the working directory.

    Thus, if the file is MyExe.exe that resides in c:\mydir, you'll do
    as[0] = "c:\mydir\MyExe.exe";
    es = null;
    dir=new File("c:\mydir");
    .... exec(as,es,dir);

  4. #4
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    as[0] = "c:\Program Files\WarCraft3\WC3.EXE";
    es = null;
    dir=new File("c:\Program Files\WarCraft3\");


    Also, remember to use "\" instead of "/" for directory's ;)

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by mtyoung View Post
    Please read Runtime Class doc
    Or else, at least search on the forum. There are thousand of answers related to your question.

  6. #6
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default

    Thanks for all the help!:D
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  7. #7
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default

    so what was the final code that works?

    and were do you find "Runtime Class doc" so you can read it?
    I did a forum search but wasted my time

    I notice you might have to go
    File file = new File("C:\\Program Files\\WarCraft3\\WC3.EXE");

  8. #8
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default

    For some reason I can not post links, but just google runtime class java, it should be the first thing to come up.
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  9. #9
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default

    So what does the code look like? that will get closer to your "post count must be 20 or greater"

  10. #10
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default

    here's the code: 9here i use the program to open audacity with password

    import java.util.Scanner;
    import java.io.File;

    public class open
    {
    public static void main (String[]args) throws Exception
    {
    Scanner keyB = new Scanner (System.in);
    String ans = "hacker";
    String trying;

    System.out.println("****************************** **********");
    System.out.println("* Anti-deleter *");
    System.out.println("* By:**************** *");
    System.out.println("****************************** **********");

    System.out.print("\nPlease enter a password: ");
    trying = keyB.nextLine();

    if (trying.equals(ans))
    {
    System.out.println("Correct password!");
    Process p = Runtime.getRuntime().exec("C:/Program Files/Audacity/audacity.EXE");
    }

    else
    System.out.println("Invalid password!");
    }
    }
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

Similar Threads

  1. Help Making a program not crash
    By Lifeis2evil in forum New To Java
    Replies: 2
    Last Post: 12-10-2008, 03:10 AM
  2. Need help with Java classes for making a program.
    By TheDarkReverend in forum Advanced Java
    Replies: 2
    Last Post: 11-28-2008, 04:50 AM
  3. [SOLVED] Making JAVA Program excutable
    By kirly in forum Advanced Java
    Replies: 10
    Last Post: 10-07-2008, 03:57 AM
  4. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 AM
  5. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 08:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •