0

I need a multi command to bring the CMD to the root of the disc it runs on.

This is how the structure looks on the USB

\data
\data\commands
\Java
\Java\bin
App.jar
App.bat

This is how the "event" looks:

 String command = "cmd /c  cd\\data\\commands && wscript \"invisible.vbs\" \"Ready2Go.bat\"";
    try { 

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

    } catch (Exception e) {
        e.printStackTrace();
    }    

This works perfekt when running App.jar file and "using" the java thats installd on the computer it self.

But when i try to run it with the java that i installd on the usb it will not work. (guide to install java on usb

app.bat file:

set Path=\Java\bin;%%Path%%
java -jar app.jar

So i need the a command to Leave the \java\bin dir. (for it is from there, I think the program is now running) and then run my command.

i have tried:

"cmd /c cd\\ && cd\\data\\commands && wscript \"invisible.vbs\" \"Ready2Go.bat\"

But without much luck. I really hope you understand what I mean

Torbjörn Dahlgren
  • 111
  • 1
  • 1
  • 11
  • just o be 100% that the app i running in /java/bin i placed a random exe file in that folder and change the code to: "Cmd /c spotify.exe" and it worked – Torbjörn Dahlgren Apr 30 '15 at 17:53

1 Answers1

0

write a startup batch start.bat like so

@echo off

rem change to this batch's folder
pushd "%~dp0"

set Path="%~dp0\Java\bin";%%Path%%
set JAVA_HOME=%~dp0\Java
java -jar App.jar

popd

and execute it as \data\commands\start.bat

Setting the current path of a process from java is done using ProcessBuilder and setting the directory-property. See a sample here

How to set working directory with ProcessBuilder

Community
  • 1
  • 1
thst
  • 4,592
  • 1
  • 26
  • 40
  • The problem is that i have around 50 bat files that i coded. The easiest thing would be to change the java file. such as "Search" after "cmd / c ...." and the Replace with "cmd / c blabla". So would like to find a solution to my command in the java – Torbjörn Dahlgren Apr 30 '15 at 17:39
  • I changed the start.bat to make sure, that the current folder of the App.jar is the path of start.bat. You can run this batch from wherever you are on the system. – thst Apr 30 '15 at 17:41
  • Do you mean that i add this to all the batch files? – Torbjörn Dahlgren Apr 30 '15 at 18:14
  • Did that, but did not do anything when pressing the "Ready2Go" button – Torbjörn Dahlgren Apr 30 '15 at 19:07
  • What is the ready2go button? Please explain a little more the chain of events. What do you want to achieve and what needs to be done? The code I presented will change directory to the place where App.bat is. What do you expect to happen from there? Who is calling the 50+ batches? The App.jar or the button? – thst Apr 30 '15 at 21:49
  • I have an app with alot of buttons that do different things,all from batch files. All the batch files is in the data/commands folder on the USB. When I press buttons, it run the command that I showed in the first thread. But with different bach files. for example RunPortableChrome.bat etc. The problem is when I run java from the USB memory, it starts not at the root (?) (USB: //) So my commando only works if I run the .jar file with Java installed on your computer. Thank you for trying to help me – Torbjörn Dahlgren May 01 '15 at 05:40
  • please add some `echo %CD%` in the batch app.bat, just before running java -jar. alo add a `System.out.println(System.getProperty("user.dir"));` to your App.java main. Now add the echo cd to one batch you run. what is the output? – thst May 01 '15 at 06:45
  • Added the Echo %cd% to the app.bat and "system..." to the java. Now before the Java -jar app.jar i now get: The "CD" command = E:\ and then i get another "E:\" after (from the java) ... But the last thing you told me to do i dont get (sorry) ... – Torbjörn Dahlgren May 01 '15 at 07:31
  • add another cd to the batch you try to run. But you are a step forward, aren't you? you are at e:\, if your batches are at e:\scripts you should be able to run cmd /c e:\scripts\command.bat the echo %cd% in command.bat should run and show you if the current dir has changed again. If cmd/c changes the cd and you cannot change all batches, you use a wrapper batch (run.cmd) that you feed the folder and script to run. inside run.cmd you cd to folder and `call script`. – thst May 01 '15 at 07:38
  • The thing is that i can not use E:\ in the java.. what if some other Pc gives the usb a different letter. what makes me most annoyed is that everything works fine just fine as long there is java installed on your computer. it's just when I must use the "java" that is installed on the USB memory the error appears – Torbjörn Dahlgren May 01 '15 at 08:10
  • That's why you use the value of user.dir to construct the path. – thst May 01 '15 at 09:17