0
votes

I am trying to execute a cmd command with java, but why does this command not work? When I try it in the windows cmd it works, but when I want to execute it with Java, it just doesn't work.

String cmd = "cd "+System.getenv("APPDATA")+"\\.minecraft"
Process pc = Runtime.getRuntime().exec(cmds);

Do you know why?

//EDIT: Error Message:

java.io.IOException: Cannot run program "cd C:\Users\Adrian\AppData\Roaming.minecraft": CreateProcess error=2, File not found... at java.lang.ProcessBuilder.start(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at at.freakey.thundriallauncheerr.Launcher$SwingAction.actionPerformed(Launcher.java:313) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.io.IOException: CreateProcess error=2, Das System kann die angegebene Datei nicht finden at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 41 more

2
Updated it with my total error log.Freakey
English only please.Pradeep Simha
Now it's english. Could you now help me please?Freakey
Error is pretty clear, do you have file at that location?Pradeep Simha
What is the exact command you run in windows?CubeJockey

2 Answers

4
votes

cd is not a program that you can execute. Even if you could it would do nothing.

When you exec, a new process is started. This new process is independent of your process (the Java process), and has it's own "current directory". Changing the current directory in that process will not affect the current directory of the Java process.

cd is a built-in command of the command-line program cmd.exe. To run a cd command, you need to run cmd.exe /c cd .... But as I just stated above, it would be meaningless (the process would end immediately).

As for changing the current directory of the Java process, see this: Changing the current working directory in Java?

0
votes

I think that exec is checking if file "cd C:\Users\Adrian\AppData\Roaming.minecraft" exists.

Instead of executing "cd C:\Users\Adrian\AppData\Roaming.minecraft" use proper version of exec method:

exec(java.lang.String command, java.lang.String[] envp,java.io.File workDir)

for example:

exec("executablefilename", null, "C:\Users\Adrian\AppData\Roaming.minecraft")