I am using the below code to access and run a bat file
Runtime runtime = Runtime.getRuntime();
Process p =null;
p = runtime.exec("cmd /c c:/{foldername}/codereview.bat");
I realized that my code is not portable as i have hardcoded the path, so i copied my bat file in a folder under /src and am now trying to use it after removing the c:/{foldername} part but
the code is not working for me... The file can not be found.
Please help as to how i can provide the relative path inside the bat file.
I did not get your exact problem but I think this may help you:
To get the absolute path of your source program(the class file executing) use this:
String path = System.getProperty("user.dir");
Now you can dynamically the path of your program and the hence the batch file(which I think is relative to the class file)
Related
I tried to reach a special path in Ubuntu, relative to the current jar file.
In Windows it is working without any problem:
String jarPath = Configuration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File(jarPath+"/../../configurationFile.xml");
However, in Linux I always get the jar file but I cannot step back two directories to the configurationFile.xml
/some/directory/where/xml/is/located/xyz.jar/configurationFile.xml: Not a directory
However, if I do
pwd /some/directory/where/xml/is/located/xyz.jar/../../
it works without any problems.
What I am doing wrong here?
I cannot figure it out.
Use only directories in your path.
After you determined the path to your jar file, extract the path to its directory and use directories only.
i tried to run an exe file from the same package the program is in using
Process p = Runtime.getRuntime().exec("prog.exe");
and
Desktop.getDesktop().open(new File("prog.exe"));
but they both give me errors. I made sure there was no spelling error in the name and it still gave me an error. is there another way to do this without having to use the entire directory?
You need to copy it from the resource which might be in a jar to an absolute path. Also make sure to get the path correct. If prog.exe is in the same directory a class in package pkg, then you need to add pkg/ to the resource name.
File tmpCopy = File.createTempFile("prog", ".exe");
Files.copy(ClassLoader.getSystemResourceAsStream("pkg/prog.exe"), tmpCopy.toPath(), StandardCopyOption.REPLACE_EXISTING);
// Needed on Linux/Mac only ---Files.setPosixFilePermissions(tmpCopy.toPath(), PosixFilePermissions.fromString("rwxrwxrwx"));
Process p = new ProcessBuilder(tmpCopy.getAbsolutePath()).inheritIO().start();
p.waitFor();
I am stuck on a how to robustly find a file path from my Java program. If I am using Linux, I can launch my program from my home folder, and then I can't say find my file at ./myProgram/myFile. Is there a good way to find my file no matter what directory my console is in?
If you are trying to access the file using the path:
./myProgram/myFile
in your program, but you aren't executing the program from the myProgram directory, then you're Java code won't see the file. Try providing it with the full path instead of the relative path. If myProgram is a directory found in your user's Documents directory then a full path would likely look something like this:
/home/*username*/Documents/myProgram/myFile
You could also build in functionality that lets you select the file by navigating through the directories and listing the files. The would provide the user with options to choose which file to utilize in the program.
You can just as well cd to the myProgram directory before executing the file and then the relative path ./myProgram/myFile should work.
Hope that helps.
Try this:
File f = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "myFile.txt");
I have a small problem calling a path(that has the python file, that I need to run) in the following code:
Process p = Runtime.getRuntime().exec(callAndArgs,env,
new java.io.File("C:\\Users\\Balkishore\\Documents\\NetBeansProjects\\Testinstrument_Rest\\build\\web"));//excuting python file
As it can be seen from the above code, the python file is called using the path specified in java.io.file function. But it is very specific, as it can be run only in my computer. How can i make it generic, so that it is possible to run this piece of code in any computer?
Any help would be very much appreciated.
Put your python script to the location relative to your working directory and use relative path. Alternatively use configuration file or property to read the path from.
If this file is already exist in the app then you need to do
ServletContext.getRealPath("/");
which will give you the path to web root now from here you need to move relatively to reach to your file
If this is an external file
put it in ${user.home}/appname/
String filePath = System.getProperty("user.home")+File.separator+"APP_NAME"
and instruct your users to put the file in this path, or read the path from some configuration file (.properties, .conf)
Is there a way to read a file from a network shared location on windows?
Let's say for instance I've this simple code that reads a text file called readMe.txt from the Addons folder.
import java.io.File;
class Sample{
public static void main(String[] ar){
File file = new File("Addons/ReadMe.txt");
System.out.println(file.getAbsolutePath());
//followed by printing the contents of file
}
}
And I execute this file using a windows batch runme.bat that has
java Sample
PAUSE
The bat runs and executes the above class only when I place the Addons folder with ReadMe.txt, Sample.class, runme.bat file in my local drive.
When it is placed in a network shared location with UNC path like \\name\Shared
In such a scenario, the bat file typically starts the base from C:\Windows and throws a classNotFoundException. I can rather map the shared drive to a *Z:* drive or whatever but I do not want to do it.
I want the code to programatically detect and retrieve the content of Readme.txt in Addons folder irrespective of whether it is being executed on a local drive or on a shared drive. Is there a way to achieve this? Please help..
Thanks
Veekay
When using a file path in Java, make sure to escape all \ correctly when giving the full path name.
For example, if the file is on PC with IP (10.10.10.123) on a Shared folder called Addons then the full path will be:
File f = new File ("\\\\10.10.10.123\\Addons\\readme.txt");
Other than the full path, your code is throwing a ClassNotFound because you JAVA-CLASSPATH is not set properly.
In your bat file %~dp0 expands to the location of the bat file. You need that in your classpath so that java can find the class, though I don't know if it will choke on UNC path. For example:
#echo off
echo %~dp0
would output
\\host\share\dir
EDIT: %dp0 will not work if there are spaces. This is what you need in your bat file:
#echo off
set p=%~dps0
echo %p%
java -classpath %p%\jarname classname
pause
Two ways of doing.
1) Map the shared path to a local drive.
2) Else hard code the server path in new File('') as mentioned by Medopal.
Something like new File("").getAbsolutePath() might help me get the base Folder when executed on a local system. Likewise there is no such way to programmatically find out the working base when executed on a shared location.