Hi
I got some trouble with my program and I need help.
this is the code:

public void actionPerformed(ActionEvent arg0) {

        Object source = arg0.getSource();

        if (source == Button1) {
            System.out.println("Player has choosen the button: " + "button1");

            String EP = JOptionPane.showInputDialog("Please enter your file location");
            String Path = EP;
            String Location = Path + "\\test.exe";

            System.out.printf("the path entered was: " + Location);

            Runtime r= Runtime.getRuntime();
            Process p= null;

            try
            {
              p=r.exec(Location);
            }
            catch(Exception e){
              System.out.println("error==="+e.getMessage());
              e.printStackTrace();
            } 

        }

        if (source == Button2) {

        }

        if (source == ButtonBattle3) {

        }
    }

so everything works fine until I paste the location
it prints the location with spaces and all that but it will not start the program
it says that the file could not be found
there are some spaces in the path I enter so if the location is:
C:\test\test 01
it only takes
c:\test\test
so how can I fix this?

In general (Windows), if you have a path with spaces in it you have to enclose the whole thing in " quote marks.

public void actionPerformed(ActionEvent arg0) {

        Object source = arg0.getSource();

        if (source == Button1) {

            String EP = JOptionPane.showInputDialog("Please enter your file location");
            String Path = EP;
            String Location = "\"" + Path + "\\test.exe\"";

            System.out.printf("the path entered was: " + Location);

            Runtime r= Runtime.getRuntime();
            Process p= null;

            try
            {
              p=r.exec(Location);
            }
            catch(Exception e){
              System.out.printf("error==="+e.getMessage());
              e.printStackTrace();
            }

        }

        if (source == Button2) {

        }

        if (source == Button3) {

        }
    }

thats how the code looks like now but now it says "Executable name has embedded quote, split the arguments"
how can I fix that?

This is a real Lulu! The hardest I've seen for some time (but here's the solution)

If you use the method exec(String s) it assumes you may have a command and some arguments in s, and it attempts to split them up using blank as a delimiter - hence the problem you see.
If you use exec(String[] s) then it assumes s[0] is the command (executable) and any arguments are already parsed into s[1] etc. In that case the executable and erguments are not parsed.

So the solution is simply to pass your executable as a one-element array, not a simple string...

     String[] dummy = {Location};
     p=r.exec(dummy);

java.io.IOException: Cannot run program "D:\location\test.exe": CreateProcess error=2,
thats all I get

and this is the new version of the code:

        if (source == Button1) {
            System.out.println("Player has choosen the button: " + "Button1");

            String EP = JOptionPane.showInputDialog("Please enter your file location");
            String Path = EP;
            String Location = Path + "\\test.exe";

            System.out.printf("the path entered was: " + Location);

            Runtime r= Runtime.getRuntime();
            Process p= null;

            try
            {
                String[] dummy = {Location};
                p=r.exec(dummy);
            }
            catch(Exception e){
                System.out.printf("error==="+e.getMessage());
                e.printStackTrace();
            }

        }

The fix certainly worked for me, running a simple .exe from inside c:\program files\
CreateProcess error 2 means "No such file or directory"
Are you certain that D:\location\test.exe is OK? Try copy/pasting that exact string into a command window and see if that works.

Given that you are relying on user input here, it would be a good idea to do some basic validation before trying the exec. eg
new File(location).exists()
will return false if the user has input an incorrect path, so you can re-prompt for correct input immediately

I am new to this part of java

        if (source == 1) {
            System.out.println("Player has choosen the button: " + "Button1");

            String EP = JOptionPane.showInputDialog("Please enter your file location");
            String Path = EP;
            String Location = Path + "\\test.exe";

            System.out.println("the path entered was: " + Location);

            Runtime r= Runtime.getRuntime();
            Process p= null;

            boolean testIfValid = new File(Location).exists();

            if (testIfValid = false){

                System.out.println("path is not valid");
                JOptionPane.showMessageDialog(null, ("path is not valid"), "and error has occoured", JOptionPane.PLAIN_MESSAGE);

            }

            else if(testIfValid = true){

                System.out.println("path is valid");

                 try
                    {
                        String[] dummy = {Location};
                        p=r.exec(dummy);
                    }
                    catch(Exception e){
                        System.out.printf("error==="+e.getMessage());
                        e.printStackTrace();
                    }

            }

        }

the boolean I have inserted is true by defeault right?
and it won't even check, because if I put "cow" as location it just ignores it and says it is valid.
"D:\Library\Apps\common\NDAIsOn\test.exe"
could it be because it has to be "D:\Library\Apps\common\NDAIsOn\test.exe"
if so are there a way to make something in a string like "\" mean "\" and how?

Bit of a problem with the boolean expressions there!
In the if tests (lines 15,22) you have = which is assignment. Java uses == for testing equal.
But in any case, because you already have a boolean, it's redundant to test it for true/false.
Here's how I would code those test for maximum clarity and simplicity:

boolean locationIsInvalid = ! new File(Location).exists();
if (locationIsInvalid) {
   // error message
} else {
   //go ahead and use location
}

Of course, you should ask the user tore-try if the inout is wrong, but naybe you can leave that on one side until the "correct" case is owrking

"D:\Library\Apps\common\NDAIsOn\test.exe"
could it be because it has to be
"D:\Library\Apps\common\NDAIsOn\test.exe"
if so are there a way to make something in a string like "\" mean "\" and how?

Sorry
1. Both those look the same to me, and
2. I don't understand the question.

I don't understand the question.

well I meant make "\" mean "\\"
but as in java the two "\" means one

I tried your code and gave me something good, the path I use wont work it gives me the error I want it to

so I guess this path wont work
D:\Library\Apps\common\NDA Is On\test.exe
or is java limited to only work on the C drive?

No, Java isn't limited to any one drive. If the path is valid Java will work with it.
Sorry, I still don't understand what you are asking about the \ characters. If you want a \ in a Java String you have to code it as \ in your Java source code. Ifthe user is inputting it in a dialog box then a \ is a \, no problems.

ps: If you are asking the user to pick a file did you look at using a JFileChooser? It's much easier for the user, and avoids problems with mis-typed paths etc.

and that fixes all of it

ok I need help with one last thing

if (source == Button1) {
            JOptionPane.showMessageDialog(null,("Please select your file"),"file", JOptionPane.PLAIN_MESSAGE);

            JFileChooser choice = new JFileChooser();
            final int chooseReturnVal = choice.showOpenDialog(null);
            File mapFile = choice.getSelectedFile();

            if(chooseReturnVal == JFileChooser.APPROVE_OPTION){
                System.out.println("Opening: " +  choice.getSelectedFile().getName());

                try {
                    Process p = Runtime.getRuntime().exec(what should I paste in here?);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

what should I paste in?

Process p = Runtime.getRuntime().exec();

mapFile.getAbsolutePath() will give you the complete path to the selected file, so just pop that in [0] of a String array and pass that to exec

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.