I,m looking for some method that can let me obtain (in windows) the directory where windows is saved (for example in my PC it will return "C:\windows".
I need it because I have to call this method
public static void openFileWithNotepad(String pathFileTxt) throws InterruptedException, IOException
{
if(System.getProperty("os.name").toUpperCase().contains("Windows".toUpperCase()))
{
String program = "C:/WINDOWS/system32/notepad.exe";
Process p = Runtime.getRuntime().exec(program + " " + pathFileTxt);
}
...
}
I want to use some method to switch "C:/WINDOWS" with the OS installation folder, in order to use this program on different pcs.
P.S.: If someone know, I'd like also to know how to use this method on UNIX OSs :)
Thank you for understanding!
Desktop.getDesktop().open(new File(pathFileTxt));
Works for any file for which there is an associated program, on any OS that supports Java 1.6+. See Desktop.open(File) for details.
i believe this should work:
System.getenv("WINDIR")
also, notepad doesn't tend to exist on unix, so i'm not sure where you are going with that...
try
System.getenv("windir")
for windows.
I'm not sure about other OSs.
System.getenv("WINDIR") may work for you.
Related
My java web application is deployed on server machine. I have a share drive which is accessible from client machine as well as server machine . Using below code i am able to open that drive on server machine :
Spring Controller code :
#RequestMapping(value = "/openAttachment", method = RequestMethod.POST)
public ResponseEntity<List<String>>
OpenFolder(#RequestParam("workflowName") String workflowName ) throws
IOException, InterruptedException {
String folderPath =Constants.workFlowAttachPath+workflowName;
if ((new File(folderPath)).exists()) {
Process p = Runtime
.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler "+folderPath);
p.waitFor();
}
List<String> msgList = new ArrayList<> ();
msgList.add(Constants.SUCCESSFUL);
return new ResponseEntity<List<String>>(msgList, HttpStatus.OK);
}
Constant used here is :
public static final String workFlowAttachPath="\\\\10.82.31.27\\Area20\\Attachment\\";
But i am unable to open this folder from local machine(browser) on local machine .
Is this possible ? If possible then how ? Thanks in advance.
No, you can't do that. If you're okay with hardcoding windows and system state into your code, you can mount that folder on a drive letter, say, drive 'N:', and then use path:
psf String workFlowAttachPath = "N:\\Attachment\\";
you can mount this on windows with something like:
net use N: \\\\10.82.31.27\\Area20 /PERSISTENT:YES
but I'm just going off of memory on that one so you'd have to check the docs on the net use windows command for the details. You should be able to invoke 'net.exe' via Runtime.getRuntime().exec() but I advise against that. You're already hardcoding IPs and who knows what in this code, might as well take care of mounting the N: drive externally too. This avoids having to deal with errors from the net command from within your java code, which is notoriously hard; windows likes to translate its commands and can throw quite a few weird errors, writing code to invoke windows utilities from java and understand their outputs is a non-trivial job.
I need some help.
I am trying to run a python script called mantime.py from a directory. I've tried to google it and found several ways to do it. Yet, I still got 2 as the exit value, which I expect it 0 (terminate normally). Here is my code:
public int performedManTime() throws IOException, InterruptedException{
ProcessBuilder pb = new ProcessBuilder("/usr/bin/python","/Users/ab/Downloads/ManTIME/mantime.py","-ppp","test",inputDir.getAbsolutePath(),"i2b2");
Map<String,String>env = pb.environment();
env.put("MANTIME_CRF_TRAIN", "/usr/local/Cellar/crf++/0.58/bin/crf_learn");
env.put("MANTIME_CRF_TEST", "/usr/local/Cellar/crf++/0.58/bin/crf_test");
env.put("MANTIME_CORENLP_FOLDER","/Users/ab/Downloads/ManTIME/externals/stanford-corenlp-full-2014-08-27");
Process process = pb.start();
process.waitFor();
System.out.println("Exit Value: "+process.exitValue());
return process.exitValue();
}
-ppp, test, input.dir and i2b2 are the arguments for the mantime.py
I tried to set up the environment as we can see above. Does anyone knows what are the problems? Any comment or suggestion would be really appreciated. Thank you
EDIT: I suspect since the python is on different directory with my tool (/usr/local/python). After I put the code bellow, somehow it works.
ProcessBuilder("/usr/bin/python","/Users/ab/Downloads/ManTIME/mantime.py","-ppp","test",inputDir.getAbsolutePath(),"i2b2");
pb.directory(/myToolsDir)
I suspect since the python is on different directory with my tool (/usr/local/python). After I put the code bellow, somehow it works. Thank you guys
ProcessBuilder("/usr/bin/python","/Users/ab/Downloads/ManTIME/mantime.py","-ppp","test",inputDir.getAbsolutePath(),"i2b2");
pb.directory(/myToolsDir)
I'm creating a simple Java wrapper for git executable, that I want to use in my app.
A small code example:
public static void main(String[] args) {
String gitpath = "C:/eclipse/git/bin/git.exe";
File folder = new File("C:/eclipse/teste/ssadasd");
try {
folder.mkdirs();
Runtime.getRuntime().exec(
gitpath + " clone git#192.168.2.15:test.git", null,
folder);
} catch (IOException e) {
e.printStackTrace();
}
}
The code simply never ends the execution.. seems like it has caught inside exec.
If I run the git clone via command line, it work as expected.
If I try another repository, from github, e.g., it works too.
Someone have a ide for what is going on here?
Thanks in advance
This isn't a direct answer to your question, but you may want to take a look at JGit, which is direct Java implementation of Git operations (no wrapping of commandline git). JGit gets a lot of use and stabilization work as it is the foundation for EGit (Eclipse Git integration).
Runtime.getRuntime().exec returns a Process object that you can use to interact with the process and see what's going on. My suspicion is that you just need to do something like this:
Process p = Runtime.getRuntime().exec(
gitpath + " clone git#192.168.2.15:test.git", null,
folder);
p.waitFor();
If not, you can also do getErrorStream() or getOutputStream() on the process to see what it's writing out; that might be helpful in debugging.
Runtime.exec() can cause hanging under various circumstances - see this article which quotes the Javadoc, which says (in JDK 7):
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input
stream or read the output stream of the subprocess may cause the
subprocess to block, and even deadlock.
The article gives some example solutions, which consume the output and error streams, although I think the ProcessBuilder class was introduced after the article was written, so may be more satisfactory: the newer Javadoc adds:
Where desired, subprocess I/O can also be redirected using methods of the ProcessBuilder class.
I want to find out how to open any exe in Windows using Java code. I have searched Google before and they only show me part of the code that they use, I think, because it doesn't seem to compile.
I have downloaded JDK 7 to compile. I don't use Eclipse at the moment and also explaining what I had to do to get it to work in detail would help a lot.
to what Sri Harsha Chilakapati said: would i need to create a class for the code?
Thanks to those who answered but i didn't quite get what you meant but i did however manage to find a website which had what i was after:
http://www.rgagnon.com/javadetails/java-0014.html
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(
"\"c:/program files/windows/notepad.exe\"");
p.waitFor();
}
}
the above was what i was after but thanks again anyway to the people who answered.
Try this.
String myExe = "C:\\MyExe.exe";
String args = "";
Runtime.getRuntime().exec(myExe + " " + args);
Hope this helps.
I would recommend the ProcessBuilder, especially for additional arguments.
How can I get the current computer's "Program Files" path with Java?
Simply by calling System.getenv(...)
System.getenv("ProgramFiles");
Notice it will only work in Windows environments, of course :-)
System.getenv("%programfiles% (x86)");
for the 32-bit folder on 64-bit PC's.
Also, it works on any language in Windows Vista and newer. Calling either of the posted responses will work on any language installation, in fact.
For 32 bit use:
System.out.println(System.getenv("ProgramFiles(X86)"));
For 64 bit use:
System.out.println(System.getenv("ProgramFiles"));
Use the System.getenv() method:
public class EnvironmentVariableExample {
public static void main(String[] args) {
System.out.println(System.getenv("ProgramFiles"));
System.out.println(System.getenv("MadeUpEnvVar"));
}
}
If the variable doesn't exist, it will simply return null.