IF statement in php Retains answer from previous run - java

I Have the following code.
$Case_Answer = 99.999;
$Compile = exec('javac Main.java');
if ((exec('java Main')) == $Case_Answer){
echo "Correct";}
else
echo "Incorrect";
My problem results when I run the script several times.
If I run this php script with all correct values and making sure that Main.java compiles. Everything works well.
When I run it a second time after modifying Main.java so it does not compile correctly. The if statement will execute whatever it did the previous run. I need it to refresh every time and it cause a problem.
Does anyone know how to resolve this?
All help would be appreciated.

If the first compile is successful and the second compile fails it still has the previous good compiled version to that runs yes? Then you may need to delete the working file before compiling again.
I'm not a Java guy so I will assume that the compiled Java file is called Main:
$Case_Answer = 99.999;
if(file_exists('Main')) { // delete it here
unlink('Main');
}
$Compile = exec('javac Main.java');
if ((exec('java Main')) == $Case_Answer){
echo "Correct";
} else {
echo "Incorrect";
}
Even if the second compile would otherwise be successful, won't it fail if the file exists from a previous compile?

Related

Java program had compilation error yet ran successfully

class Return
{
public static void main(String args[])
{
boolean t=true;
System.out.println("Before the return");
if(t)
return;
System.out.println("This wont execute");
}
}
This program is from Herbert Schidt. I tried to run this program using command prompt without if(t)
to see the compilation error
error: unreachable statement
System.out.println("Wont Execute");
I understood this error but program is running fine when i execute the command java Return.
It shows the output
Executes
So I wanted to know how this program is running even with compilation error?
You were almost certainly running the last successfully compiled version of this class. When you execute the java compiler, it doesn't erase its old output, it overwrites it. So if you compiled Return.java once successfully, you'll have a Return.class on your disk. If you alter Return.java to be uncompilable and try to compile it, the Return.class from the previous successful compile will still be there.

Grab or know a error occurred in a a jar?

I am trying to write a program that calls external jars from the command line. In my code it will do java -jar test,jar args. What I want to know though is if a error occurs in this external jar, how to catch it in my java program so I can do the necessary procedure? This is a new zone of coding for me from college level so I am a little clueless.
Command-line programs returns exit status when finished executing it's work (e.g. zero when everything is ok).
You should be able to retrieve something interesting by storing the return value of your system call and test it according to what you want to do.
// Code from https://stackoverflow.com/questions/8496494/
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar test.jar args");
// Check retVal to test
int retVal = pr.waitFor();
More about this in this SO question.

Read and write to cmd in windows

I used system("java .....")to run a java app in cmd with VC++ code.
The java app will run a server in the cmd,it will output info in the console.And I can also enter commands to it just like run "dir" commands in cmd.
Now I want get all the output in my program and use C++ code to write commands sent to the java app.
But I found that the system() won't return until I stop the java app.It's reasonable.And how to avoid it?Use Thread ?
And the biggest problem is I don't know how to get the output and write commands,can anyone give me a method?
Thanks a lot!
P.S. The java app's code can't be changed.
--------------------------------------I have made progress--------------------
int main()
{
char psBuffer[256];
FILE* output = _popen("java xxxx.jar", "rt" );
if(output == NULL)
return 0;
while(fgets(psBuffer, 256, output))
{
printf(psBuffer);
}
if (feof( output))
{
printf( "\nProcess returned %d\n", _pclose( output ) );
}
else
{
printf( "Error: Failed to read the pipe to the end.\n");
}
system("pause");
return 0;
}
When I use "dir".It works perfect!But when I use java,psBuffer is always nothing,and the output of java app is normally.Is it pipe cannot redirect java's output?
I change my code and make some java command run perfect:
FILE* output = _popen("java -version 2>&1", "rt" );
But when it run that .jar,It failed.I read the .jar's code and find the output is create by java.util.logging.Logger.info().I'm not familiar with java. How dose the info() work in cmd?
Thanks many!
Finally, I found last code above is work correctly.But origin output of java app haven't been redirect .It will display normally,but buffer is correctly received the output I want.Anyway I solved my problems.Thanks everyone!!!
The MSDN article Creating a Child Process with Redirected Input and Output explains how you can do it. It is quite a lot of code to go through, but will allow you to do what you want, and give you full control over it.
On the other hand, using _popen is much easier, but you don't have as much control. Depends on your exact needs as to how much code you'll be writing :).

Runtime.getRuntime().exec strange behavior

I am trying to call a perl script from java runtime. It worked fine on my windows7 laptop with the following code,
try {
String cmdString= "c:\\perl64\\bin\\perl.exe c:\\perl64\\eg\\userinput.pl \""+arg1+"\" \""+arg2+"\"";
Process p = Runtime.getRuntime().exec(cmdString);
} catch(IOException e) {
System.out.println(e);
}
The perl script runs and produces what I expect (update database).
When I move the whole thing over to a remote CentOS server, it doesn't work anymore. The script is the same and the java code is,
try {
String cmdString= "/opt/lampp/bin/perl /home/support/scripts/userinput.pl \""+arg1+"\" \""+arg2+"\" > /tmp/userinput.log";
log(cmdString);
Process p = Runtime.getRuntime().exec(cmdString);
} catch(IOException e) {
System.out.println(e);
}
I added redirect to /tmp/userinput.log after I see the script is not working. But there is no log file created at all. I also added log to make sure this part of the java code did get executed, and indeed it did. I also tried to add "/bin/bash " in front of the comString and it didn't make a difference. However, when I run the cmdString directly on the remote server from command line, it works without problem.
Now, when I changed the cmdString to "touch /tmp/userinput.log", it does create the empty log file.
So I know the Runtime.getRuntime().exec(cmdString) command ran, and the cmdString works when entered on command line, and a simple "touch" command would work with this setup. But I am totally lost why the actual cmdString that calls the perl script doesn't work, and there is no message whatsoever to tell me what is wrong.
Can someone please help?
Frist, separate each parameter for the command and use the version of exec which takes a String[] (you won't have to worry about quoting issues). also, shell redirection won't work since java isn't executing a shell.

Java Virtual Machine Launcher Error when presents particular piece of code

I have a code that that reads parameters from XML file. In debugger everything works fine, but after I built JAR file and run it - I get the following window
Java Virtual Machine Launcher
A Java Exception has occured.
But if I comment this piece of code:
else if (settingName.equals("log_level")) {
String value = element.getAttribute("value");
if (value.equals("full")) {
modelLogLevel = EnLogDetails.LOG_FULL;
} else if (value.equals("apdu")) {
modelLogLevel = EnLogDetails.LOG_APDU;
} else if (value.equals("none")) {
modelLogLevel = EnLogDetails.LOG_NONE;
} else {
throw new InvalidArgumentException(new String[]{"log_level"});
}
}
and rebuild JAR again - it works fine. How to fix this issue?
You've provided exactly none of the relevant information, such as the actual exception or the command line you are using, but clearly you are supplying a log_level argument that doesn't match any of those tests, so you are throwing an IllegalArgumentException which is terminating main() and therefore the launcher. If so it is a complete mystery to me why you need to come to StackOverflow to have your own code explained to you.
Or else the references to EnLogDetails are failing in some way which is shown in the exception which you haven't supplied.

Categories

Resources