How can I get volume id on Windows using Java? - java

I'm developing a java desktop application to access information, format, change labels and volume id. This app's target are Windows users that will be using mainly Windows XP or Windows 7.
I'm getting basic info using java's File and FileSystemView; and I'm using Runtime.getRuntime().exec() to execute external Windows applications to do the other tasks.
I tried to use 'vol' and 'dir' to get the volumeId information but I get the following error:
Starting: vol E:
java.io.IOException: Cannot run program "vol": CreateProcess error=2, O sistema não pode encontrar o arquivo especificado
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at app.CommandRunnerWithReturn.run(CommandRunnerWithReturn.java:24)
Caused by: java.io.IOException: CreateProcess error=2, O sistema não pode encontrar o arquivo especificado
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Is there a Java API or Windows program I can use to get volumeid info?
Thanks in advance.

Some commands are built in to a shell. To run these commands you need to run the shell to run the command. For CMD you need something like
CMD /C VOL
for unix shell you need something like
sh -c "cd /path ; command > file"

Related

How to call an external application if using exec() defines it as invalid Win32 application in Java?

I'm fiddling with opening external applications from Java source code. I am trying to open a launcher for a game called Runescape, which is found inside C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Jagex. The name of the file inside this directory is RuneScape Launcher.url.
This is the code which demonstrates my progress so far:
public static void main(String[] args) throws IOException, InterruptedException {
//doesn't work
Process p = Runtime.getRuntime().exec("C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Jagex\\RuneScape Launcher.url");
//if Chrome was to be opened, it works, since it is .exe
// Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
p.waitFor();
System.out.println(p.exitValue());
}
The error that gets thrown is:
Exception in thread "main" java.io.IOException: Cannot run program "C:\ProgramData\Microsoft\Windows\Start": CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Main.main(Main.java:47)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Obviously, RuneScape Launcher.url is not a valid Win32 application. How to start such an application?
My research:
- this post suggests using ShellExecute, however it is written in another programming language. I couldn't find a similar solution for Java.
- this post talks about passing parameters when calling an external application, but that external application is .exe
- this page demonstrates calling external applications, but again only .exe
Then, I've tried starting this launcher from cmd manually ... successfully. First, I've located launcher directory: cd C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Jagex and then called the launcher: "RuneScape Launcher.url". This started the launcher correctly. Why doesn't it start from Java code?
Try passing the launcher as an argument to cmd.exe:
Process p = Runtime.getRuntime().exec("cmd.exe", "/c", "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Jagex\\RuneScape Launcher.url");

Java error in making file through console

I want to make a file though the cmd in java using this code
Runtime.getRuntime().exec("mkdir C:\\Users\\Nick\\test");
and i get this annoying error:
Exception in thread "main" java.io.IOException: Cannot run program "mkdir": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at LFID.main(LFID.java:11)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
I have no idea what's causing it so help.
By the way please don't tell me how to create a folder not through cmd, I need to do it this way. Thanks.
mkdir isn't a standalone executable you can launch as a separate process - it's a command that the Windows command shell understands.
So you could run cmd.exe /c mkdir ...:
Runtime.getRuntime().exec("cmd.exe /c mkdir c:\\Users\\Nick\\test");
Or:
Runtime.getRuntime().exec(
new String[] { "cmd.exe", "/c" "mkdir" "c:\\Users\\Nick\\test"});
... but I'd still recommend just using File.mkdir instead... why call out to an external process when you can do it within Java? (If you're going to specify an odd requirement, it helps to give some more context on it...)

Execute Shell Script using Http path in java program

i have a path where i have shell script which i have to execute using java program but i am getting error as .
Runtime.java
public class Runtime {
public static void main(String[] args) {
System.out.println("Triggered");
try {
Process p = Runtime.getRuntime().exec("\"http://192.168.1.7/sh_scripts/check_process/2.sh\"");
System.out.println(p);
} catch (IOException e) {
e.printStackTrace();
}
}
}
LOGCAT
http://192.168.1.7/sh_scripts/check_process/2.sh: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.howtodoinjava.demo.poi.Runtiime.main(Runtiime.java:15)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Did i miss anything while executing . The path is not geeting read from the java program to execute the shell script So please help me to execute the jar file using the given http link where i can execute the 2.sh file successfully using java
As far as I know, you can only execute an executable FILE on the same computer, there is no support for downloading a script from a remote server in http, ftp or whatever else.
Same is true also in a normal unix shell.
So, you have a few options :
Download the .sh file yourself in java, put it in a temporary file, and execute it there
Run another .sh file that downloads the .sh file from http and executes it; can be a oneliner, like source <(curl -s http://192.168.1.7/sh_scripts/check_process/2.sh)
Try to execute this oneliner, maybe using ProcessBuilder can ease the creation of the redirection etc.. but I'm not sure.

XMLBeans: java.io.IOException: Cannot run program "C:\xmlbeans\bin\jar":

I'm trying to run XMLBeans:
scomp -compiler "C:\Program Files (x86)\Java\jdk1.6.0_37\bin\javac.exe" -cp "C:\Components\*";"C:\Components\jsr173_api.jar";"C:\xmlbeans\lib\xbean.jar";"C:\xmlbeans\lib\xbean_xpath.jar";"C:\xmlbeans\lib\jaxen-1.1-beta-2.jar;" -out S2002PDPIn.jar S2002PDPIn.xsd
It gets an error below:
java.io.IOException: Cannot run program "C:\xmlbeans\bin\jar":
CreateProcess error=2,
The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at org.apache.xmlbeans.impl.tool.CodeGenUtil.externalJar(CodeGenUtil.java:304)
at org.apache.xmlbeans.impl.tool.SchemaCompiler.compile(SchemaCompiler.java:841)
at org.apache.xmlbeans.impl.tool.SchemaCompiler.main(SchemaCompiler.java:272)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 6 more
BUILD FAILED
At first I thought the "C:\xmlbeans\bin\jar" it was looking for was a directory so I just created a folder "jar", but when I run it again the "jar" was referred as a file where I get error:
java.io.IOException: Cannot run program "C:\xmlbeans\bin\jar":
CreateProcess error=5, Access is denied
Please kindly help me how to fix this so that it don't look for "jar" file.
I looked "scomp.cmd" but there is no line that will do this.
Thank you in advance.
You are simply calling more applications, than you expect:
scomp -compiler "C:\Program Files (x86)\Java\jdk1.6.0_37\bin\javac.exe" -cp "C:\Components\*";
"C:\Components\jsr173_api.jar";
"C:\xmlbeans\lib\xbean.jar";
"C:\xmlbeans\lib\xbean_xpath.jar";
"C:\xmlbeans\lib\jaxen-1.1-beta-2.jar;" -out S2002PDPIn.jar S2002PDPIn.xsd
This happens because of ";", you would have to enclose the complete classpath with one " and seperate the values by ;.

executing Runtime.getRuntime.exec(String cmd)

Hi
I am trying to execute the command string with Runtime.getRuntime.exec(String cmd).What
i'm actually trying to extract the I frames from video using the MPlayer and it is
installed in the different directory than that of my eclipse workspace.
I'm using the java code like the below
C:\\\Program Files\\\MPlayer for Windows mplayer file.mp4 -benchmark -noframedrop -ao null -vo jpeg:outdir=iframes -vf framestep=I
the actual command to extract using DOS is
"mplayer file.mp4 -benchmark -noframedrop -ao null -vo jpeg:outdir=iframes -vf framestep=I".
The ECLIPSE WORKSPACE is in my d: drive.And the Mplayer is in c:\program files\Mplayer for windows.
Eclipse IDE shows the exception as:-
Exception in thread "main" java.io.IOException: Cannot run program "C:\Program Files\MPlayer for Windows": CreateProcess error=5, Access is denied
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at myvideo1.main(myvideo1.java:39)
Any help is greatly appreciated.
Try this:
exec("\"C:\\Program Files\\MPlayer for Windows\\mplayer\" file.mp4 -benchmark -noframedrop -ao null -vo jpeg:outdir=iframes -vf framestep=I");
I surrounded the command in \" since the path contains spaces and added a missing \ in front of mplayer.

Categories

Resources