VB.Net How to run the application in a specific folder? - java

So a little bit of my code is to run an application (a batch file in an external folder that the user can select.) The problem is that when I use
Path.GetDirectoryName(O.FileName) + "\BasicStart.bat"
however when I try to do the above it fails to open to java application due to the class folders trying to be referenced from where the vb.net application is launched
The code in the .bat is
#echo off
#title JavaApplet12
set CLASSPATH=.;dist\*
java -client -Dnet.sf.odinms.wzpath=wz server.Start
pause
And I know I can hard set the classpath and wzpath but users of my application will not want to do this.
How can I launch it in vb.net so that the application is launched from that location and can read the files from that folder location.

This should be resolvable by setting the working directory of the process before you start it:
Dim processStartInfo = New ProcessStartInfo()
processStartInfo.WorkingDirectory = Path.GetDirectoryName(O.FileName)
processStartInfo.FileName = "BasicStart.bat"
Process.Start(processStartInfo)

Related

Cannot locate .xml file using WinSW to start windows service

I am trying to use Winsw to make my jar file run as a windows service. I rely on the documentation in order to do so.
To install a service, it is specified that the command winsw install myapp.xml should be used. The problem is that when I do that I get the error :
> Winsw.exe install myapp.xml
2022-11-15 12:24:46,791 FATAL - Unhandled exception
System.IO.FileNotFoundException: Unable to locate Winsw.[xml|yml] file within executable directory
at WinSW.Program.LoadConfigAndInitLoggers(Boolean inConsoleMode)
at WinSW.Program.Run(String[] argsArray, IServiceConfig config)
at WinSW.Program.Main(String[] args)
it expects the XML/YML to have the same name, when I rename the XML file it works fine, but I do not want to rename it as I want to use the same .exe file in order to start multiple services.
I tried to launch the different commands of install/uninstall, they all react the same.
Any Ideas ?

Exception in thread "main" java.nio.file.AccessDeniedException when creating a file [duplicate]

I have a Java application were the user can create a text file and save it wherever he wants on his computer using this code :
File txtFile = new File( path );
Writer writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( txtFile ), "UTF-8" ) ); // Error occurs here.
But many users using Windows 7 reported that when saving the file to "C:\", they get "Access is denied" error. I found that this is because they need administrator permissions to save the file in such path in Win7.
Instead of showing a warning message to the user: " You can't save the file at this path ", can i save the file in this path somehow, like if there is a way to have Administrator permissions in Win7 through Java code, or something like that ?
Short answer - no.
If you need to save to C drive, they need permissions. If this program just needs to create files, you can use the users temp folder. See System.getProperty()
Windows Vista and Windows 7 have UAC enabled. UAC denies creating new files in SOME locations, without administrative privileges.
Check your permissions and make sure to execute the java executable in ADMINISTRATIVE Account, OR disable UAC.
To do that, go to "Start" type in "CMD.EXE" -> right click on the cmd.exe file and Run As Administrator. Then navigate to the location containing the .class file. Then type in java ClassFile and hit enter
Are you using cmd , i.e. Dos to run your file or eclipse? Whatever you are using
It looks like you are running as default user.
in windows 7, UAC by default blocls writimg to system.directory.
Do the following and hopefully it should work1
> If cmd.exe
> Then when you open run from start menu. Right click it, select run as administrator and then run your application
>
> If eclipse/any other IDE
>
> Close existing, right clicl eclipse,select run as admim and then run your application
>
> Hope this helps

Open a file on Openshift using Java

I have a DIY cartridge. My project structure looks like
MY_PROJECT
-diy
-myProgram.jar
-resources
-file1, file2...
-.openshift
-action_hooks
-start
The myProgram.jar uses files from the folder 'resources'.
The code looks like
File imageFolder = new File("resources");
System.out.println("Image Folder read:"+imageFolder.canRead()); //canRead returns false
File[] listOfFiles = imageFolder.listFiles(); // here I get null
The program runs by action hook 'start':
nohup java -jar $OPENSHIFT_REPO_DIR/diy/*.jar --server.port=${OPENSHIFT_DIY_PORT} --server.address=${OPENSHIFT_DIY_IP} &
The problem is that I'm not able to work with files.
As described in code comments I get null on listFiles().
If I run the program on Openshift manually(ssh to server/$OPENSHIFT_REPO_DIR/diy/ and run java -jar ...) it works, but it doesn't work via action_hooks.
Thank you in advance!
I resolved the issue with Openshift env variable 'OPENSHIFT_REPO_DIR'.
Instead of using relative path
new File("resources");
I use absolute
String absolutePath = System.getenv("OPENSHIFT_REPO_DIR");
new File(absolutePath + "diy/resources")

Unable to Load Dependent SO file in LInux

I am new to linux. I am trying to load a SO file in Ubuntu using Java. The file that I have specified in the java method "System.load(/home/ab/Downloads/libtesseract.so)" loads fine but its dependent so file placed in the same place as "libtesseract.so" is not found. Here is the error message I get. Error: UnSatisfiedLinkError and says "liblept.so.4" cound not be found. This so file is placed in the same location as libtesseract.so. When I place "liblept.so.4" in the "/lib". It is able to load this so file from. So what I understood is that for, its not for java to load the dependent so. It has to be loaded by ubuntu. So I tried a simple application to load this by setting the PATH variable with the location of the so file. And exported the java code into a jar and tried to run this jar file from terminal as the path variable is not persistent for entire system. It worked fine. So I tried to do the same thing programmatically by using the code below to its not working. Please advice. TIA
Code:
ProcessBuilder pb = new ProcessBuilder("/bin/sh");
Map<String, String> envMap = pb.environment();
envMap.put("LD_LIBRARY_PATH", "/home/ab/Downloads");
envMap.put("PATH", "/home/ab/Downloads");
Set<String> keys = envMap.keySet();
for(String key:keys)
{
System.out.println(key+" ==> "+envMap.get(key));
}
System.load("/home/ab/Downloads/libtesseract.so");
As far as I know you can't really modify the environment variables in Java "on-the-fly". That means you should set both LD_LIBRARY_PATH and PATH before running the java.

java, execute exe from WAR in java code

In my war, I have file exe in WEB-INF\classes\
How can I execute this file in Java code (How can I specify path to this file) ?
command = " ? ";
Process x = p.exec(command);
Te following approach could work:
1) Prepare full path of your executable:
ServletContext context = getContext();
String fullPath = context.getRealPath("/WEB-INF/classes/executable");
2) Execute like you would normally do it:
String[] cmd = { fullPath /*[...] arguments */};
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
This is a simplified example; you may also want to read more about ProcessBuilder.
This is bad idea. Imagine simply fact that your .war packege should run on almost any server (".war is platform independend") and your .exe file is compiled just for one architecture.
Better should be execute your .exe as external program just for separate platform independent and platform dependent part. Then in java you can test operating system and on this basis run desired externel programm.
Read this link with similar question.
The best way to do find a file's real location inside a web app is to use the ServletContext.getRealPath (see http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String))
You can access that object from the session...

Categories

Resources