running C++ exe from servlet - java

I am running a server on my machine. When Servlet receives a message, the corresponding Visual C++ ".exe" need to start running.
I am using following code to start the exe. But I am getting "Microsoft Visual C++ Debug Error". The code is as follows:-
if(strLine.equals(location))//same place do not do anything
{
Runtime rt=Runtime.getRuntime();
String cmd[]={"cmd.exe", "/c", "C:\\Users\\nabeel.OUCS1289\\Documents\\Visual Studio 2010\\Projects\\Scene Localization - (FM)\\Debug\\Scene Localization.exe"};
rt.exec(cmd);
System.out.println("Same place so dont do anyuthing");
}
Please help me out in this regard.

The EXE file to be executed is located in a user profile directory. Does the account running the JRE/Webserver does have read & execute permissions on that particular directory?
Furthermore remove the indirect execution via cmd.exe /c .... This is total unnecessary for regular executables. It is only required in case you are executing a command that is provided by cmd.exe itself and therefore can not be executed via an exe file.

Related

Running Java program in a Docker Image

So I have a Docker network that has a Docker file with a bunch of information. I have a java program that is going to bring up the enviorment and then produce several commands to run within this enviorment. To be clear, the first command I need to run is NOT inside the Docker enviorment. I am having some challenges with the Process and Runtime classes.
First, say I wanted my java program to launch a new gnome terminal and then run a command to get into the docker network. I have this command,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal"});
Gnome terminal sucessfully comes up but any additional arguments I give in this array are just ignored. For example,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","ls"});
Does not work. The command I ultimatly want to run would look something like this,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
Second, Once I have this running, will additional commmands I run work within the Docker enviorment? I have a python file with a Stream handler that specifies the correct commands to run.
Other documentation on related issues was limited.
I made sure my code was wrapped in a runtime exception try catch and that I was running the correct .class file. Any help on this would be great!
Edit: I have also tried to run this in another linux terminal like Hyper and Tilda
I also am able to get a sudo sign in when I run the command like so,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","--","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
However it closes immediatly after authorizing.
Okay this is what I was attempting to do.
https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome
This site is outdated and I had to use this link for getting that latest version of the java PPA.
This process basically installs java into the docker contatiner so that I can run a java program that uses Runtime.

launching java program via batch and php,

php and batch files.
So i'm executing batch files through php
I have no issues launching the batch.
$str = exec('C:\WINDOWS\system32\cmd.exe /c START C:\MInecraft\_restart.bat');
However the issue im having is getting the batch file to work currectly. When the batch is run, it executes this code
taskkill /IM java.exe /F
java -Xmx1024M -jar craftbukkit.jar -o true
batch file successfully runs the taskkill command when launched with php, however it will not run the next line. When ran manually it launches fine, (bear in mind that the cmd.exe does not exit it stays open with this code)
Any ideas on how i can get this to launch from php?
Maybe the reason is the same as given at Error Executing Batch file of BlazeDS, the java executable is not found by Windows on running the batch file from within PHP script.
The current working directory or the directories listed in environment variable PATH can be different when the batch file is executed from within the PHP script in comparison to running the batch file manually.
Do you have ever tried to specify java.exe with full path in double quotes in the batch file?
You could also add at top of the batch file the commands
dir /w
PATH
to see which directory is the current directory and which directories are listed in environment variable PATH on execution of the batch file from within PHP script.
Forgot to post back here, I specified java through windows path command rather than the batch. The problem is because the java process takes the cmd and turns it into a console, it never finishes the batch file. So, the php never returns and continues the code unless the batch is force ably closed on the server.
pclose(popen("start /B C:\MInecraft\_restart.bat &", "r")); die();
I ended up using this command, which i believe makes it launch the batch and not wait for a reply. Just carry on with the php.
Thanks for your kind reply. I did initially try launching java in multiple ways.

Running a JAVA program as a scheduled task

I am trying to run a simple JAVA program once per day on a Windows 7 machine.
My code runs fine inside NetBeans. If I do a clean and build it suggests this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This does not work from the DOS prompt of course because of the space between program and files so I do this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This works from the DOS prompt.
I now create a task in Windows Scheduler to run:
C:\Program Files\Java\jdk1.7.0/bin/java
with arguments:
-jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
When I then run it, all I see is a DOS box flashing up for a second. I expect the code to take about 30 secs to run. The code should persist data to a database and no updates happen.
The code also uses java.util.logging so I should see log entries and I don't.
I strongly suspect that I am not running the JAVA command properly or that there's a bad classpath issue that it present when running via Scheduler that isn't there when running from the DOS prompt.
Help would be appreciated. If you've seen this before and can sort it that would be great. If you can tell me how to get a meaningful error trace from Scheduler than that would also be really helpful.
Thanks!
I Think that you could create a simple batch script that will launch your program in this way :
#echo off
REM Eventually change directory to the program directory
cd C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\
REM run the program
"C:\Program Files\Java\jdk1.7.0\bin\java.exe" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
Copy it into the notepad and save as java_script.cmd and then schedule this script instead of the program directly.
I solved it after changing all fonts' references to "SansSerif"
I was using Jasper Reports inside Java to create a PDF file. It was working fine when I double click the batch file or Scheduler with Windows Server 2003 but not working with the Scheduler of 2008.
I tried many different things nothing worked so I though Could it be that Windows Server 2008 is blocking the access?.
Now is working perfect. So, if you are having problems check the references to anything you are using.
The scheduler will run under a different user unless you specify what user to run as. If it isn't running as your user then it won't be able to write to your directories.
The real problem to the original question is a java installation issue on Microsoft systems. Java jre installs into Program Files\java. The executable (java.exe) is only installed in that java\bin directory. Running from the command line, the os looks in the proper location for the java.exe. Running from other MS tools (such as VBA Excel or in this case TaskScheduler), it does not!
You can see that TaskScheduler is looking in the wrong place by viewing the tasks history in the TaskScheduler tool. Double click on some of the history events and one will list the action and return code. The action will show that the TaskScheduler is trying to run
"C:\Windows\system32\java.EXE"
So, copy java.exe from the java\bin directory into the place where the scheduler is looking, and now it will work.
Or update your task and provide the full path to java.exe.
You can also update the environment system path to look for java in the java\bin directory, but that has to apply to all users and sometimes this is faulty as well.

Runtime exec issues in Linux

In an application I am writing, I am launching another application (a runnable JAR) using Runtime.exec(...). Everything launches successfully in Windows, but Linux (specifically certain installations of CentOS - works in Ubuntu) has been giving me some problems. For some reason, the only way the secondary application will successfully launch is if I execute the first through a terminal. All behavior works as expected. However, if I launch the first application by double-clicking its icon (without a terminal open), the button to launch the second application seems to do nothing. I get no exceptions or error output - just a flash of my progress bar saying that it is launching, and then nothing. I can confirm through jconsole that the second application's process is never launched.
I have seen the commonly linked article on the pitfalls of the exec method ( http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html ), but have not been able to solve this problem with anything I have found there. I am in fact reading the output and error streams of the second process, as I see all output when it successfully runs (after launching the first application through a terminal command). Not knowing a lot about deeper workings of Linux, I think this sounds like it may be a permissions issue with the output stream or something, but I am not sure.
In case it helps to diagnose the problem, I am using the command:
rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."}); Since this works (depending on how the application is launched), I'm not too concerned that anything is wrong with this piece of code...
Anyone have any suggestions? Thanks in advance!
EDIT: The solution was to fix the directory to the JAR I was attempting to run. When launched via the GUI, user.dir was pointing to the parent directory of the folder containing my application. Since I'm using Eclipse RCP, my solution was to use
String currDirPath = Platform.getInstallLocation().getURL().toString(); instead. Thanks for the help everyone!
Since you're just using the jar file name - myjarfile.jar - and not the full path to it, depending on the current working directory, the jar may or may not be found. Try changing your exec command to use the full path to the jar instead. You can debug this by using rt.exec() to write the output of 'pwd' to a text file.
instead of
rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."});
use
rt.exec(new String[]{"\bin\bash", "-c", "/***path to java***/java -jar myjarfile.jar myArg1 myArg2 ..."});

mkdirs not working in windows 7

I made a small java app that copies a directory from a CD to the HD. I made the program using Windows Vista and it worked, but when i ran it in Windows 7, it fails.
The main problem is that a folder inside the Program Files folder needs to be created.
I used DestinationFolder.mkdirs(), but it fails creating it
This is the java code:
public void Install_App()
{
File srcFolder = new File(System.getProperty("user.dir") + "\\WINDOWS");
File destFolder = new File("C:\\Program Files\\test1\\test2\\");
if (srcFolder.exists())
{
try{
if(!destFolder.exists())
{
destFolder.mkdirs();
}
copyFolder(srcFolder,destFolder,1);
}catch(IOException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.toString());
error=true;
System.exit(0);
}
} else
{
JOptionPane.showMessageDialog(null, "Error. Source Directory doesn't exist.");
error=true;
};
}
... and then there is a copyfolder function that copies the files with inputstream and outputstream.
The problem is that the folder is never created. My login user is an administrator. And as i said, it worked in Vista.
Could you help me, please?
Thanks.
The thing is that i created this app in java to run it in Windows and Mac.
In Windows it should autorun with and autorun.inf like this:
[autorun]
OPEN=java_app.bat
then this bat will run this:
#echo off
start javaw -jar "java_app.jar"
EXIT
so how can i modify it to run it as administrator automatically?
The main idea of this java app is simplify the process of install & use an external application no matter which OS are you using. If I have to ask the user to run it as admin it will loose it's sense (of been simple of use).
I am guessing you are running your code as regular user.
Writing into Program Files directory as a regular-user is by default blocked by UAC under Windows 7. That's why your Java code fails to create directories.
Try running your Java code from a privileged shell. You can have one by Start > [type cmd] > [right-click on 'cmd.exe' and select "Run as administrator"]. Now, run your compiled code with java -jar or java -classpath from the administrator command prompt. It should work now.
Automating the UAC prompt:
You need to create a manifest file as described in detail at [1] and [2] to let Windows/UAC know that your program would need elevated privileges.
Also check this [3] utility called elevate that would spawn your program as child process while handling the UAC permission requests all being made from the parent (elevate) program itself.
[1] [http://msdn.microsoft.com/en-us/library/aa511445.aspx][2]
[2] [http://msdn.microsoft.com/en-us/library/bb756929.aspx][3]
[3] [http://www.wintellect.com/cs/blogs/jrobbins/archive/2007/03/27/elevate-a-process-at-the-command-line-in-vista.aspx][4]
This is all the permission problems. I have the same problem on my machine. Nothing is wrong with your java code. I tried to create folder using command line and got "Access Denied".
C:\Users\alexr>mkdir "C:\Program Files\mytest"
Access is denied.
So, the solution is whether to create folder in other location or run as administrator. As #Alex K. aready said, refer to this post to learn how to get such permissions.
Windows 7 Create Folder in "Program Files" failing in C# code even thought I have admin rights!
You do not have the proper privileges to create directories in Program Files. You must start the application with administrative privileges.
An important thing to learn is that when you are developing your applications you should never write them to save/modify data inside Program Files; instead they should either write to AppData our My Documents.
Modifying files in Program Files has been severely deprecated ever since Windows Vista, and even earlier than that. You should try and follow this rule from the start, or it means headaches to rewrite your entire application if you ever want to publish it online.

Categories

Resources