I use this to compile my java file
$command_compile = "javac $target_path_file 2>&1";
exec($command_compile, $compile_result, $return_compile);
And When my java file has syntax error. It always has error
Error: Could not find or load main class Myclass
How to get error such as Syntax Error. need ) On line 5
Thanks you
$res = exec($command_compile, $compile_result, $return_compile);
inside $res you get the last line of output of the executed command
If javac returns more lines (either error or informative messages) you may want to parse the $compile_result array that contains every output line.
foreach($compile_result as $compile_result_line) {
// do what you need with $compile_result_line
}
Anyway see http://php.net/function.exec for a detailed explanation of exec
The error you get is not from PHP code but is a "java" issue when you call javac. It seem you're trying to compile a file that reference an undefined class.
Related
So my PHP file is meant to check if a variable is null, if so than echo and output, and stop there
Here is that code:
if(is_null($ip)){
echo "IP is not valid";
clean_all_processes();
}
So when I try to test this script using the insomnia rest client it outputs the "IP is not valid" but also gives a "500 internal server error"
In my error_log file it spits out this every time
Uncaught Error: Call to undefined function clean_all_processes()
Note: I am using php 7.3
There is no such function called clean_all_processes() in PHP. The answer you linked to used it as an example name of a function you could call.
If you want a hard stop of your script use die(). This is not recommended! You should structure your code in such a way that you should almost never need to use this approach.
There is no way to break out of if statement, because such thing makes no sense. An if statement is already a condition. You either execute the code or don't.
What actually i want to do is to get contents from the winword file in my web page.
for this purpose i used the exec() method, i also tried the following code:
$pCom = new COM("WScript.Shell");
$pShell = $pCom->exec("Notepad.exe");
$sStdOut = $pShell->StdOut->ReadAll; # Standard output
$sStdErr = $pShell->StdErr->ReadAll; # Error
echo($sStdOut);
the above code through an Exception that:
Fatal error: Class 'COM' not found in D:\xampp\htdocs\test\tests\Notepad.php on line 9
I have no idea what to do and how to do this?
You have two issues with your approach:
a text editor does not output text written in it when closing the editor. So why would you expect to receive the text when firing the editor by means of an exec() call?
you miss understood how the exec() command actually works, I assume you did not really read the documentation of the exec() function which clearly states that the return value of an executed command is the last line of its output. That is not what you want.
I'll give you the gist.
I'm trying to scrape certain URL's using a third party HTML tag stripper because I don't think the default strip_tags() does the job well. (I don't think you need to check that scraper)
Now sometimes, the HTML source code of some sites contains some weird code that is causing my HTML tag stripper to fail.
One such example is this site that contains the following piece of code :
<li>Photo Galleries</li>
that causes the above mentioned tag stripper to throw this error :
Parse error: syntax error, unexpected
T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or
T_NUM_STRING in /var/www/GET
Tweets/htdocs/tmhOAuth-master/examples/class.html2text.inc(429) :
regexp code on line 1 Fatal error:
preg_replace() [<a
href='function.preg-replace'>function.preg-replace</a>]:
Failed evaluating code:
$this->_build_link_list("<//?=$cnf[\'website\']?>girls/models-photo-gallery/?sType=6#top_menu",
"Photo Galleries") in /var/www/GET
Tweets/htdocs/tmhOAuth-master/examples/class.html2text.inc on line
429
Now what happens is, there is an array of many URLs and some throw the abovementioned error. I do some processing on each URL.
If some URL in the array throws an error like this, I want the execution to proceed ahead with processing of next URL without it disturbing anything. My code is something like this:
foreach ($results as $result)
{
$url=$result->Url;
$worddict2=myfunc($url,$worddict2,$history,$n_gram);
}
Here myfunc does the processing and uses the 3rd party HTML stripper I mentioned before.
I tried modifying the code to this:
foreach ($results as $result)
{
$url=$result->Url;
$worddicttemp=array();
try
{
$worddicttemp=myfunc($url,$worddict2,$history,$n_gram); //returns the string represenation of what matters, hopefully
//The below line will be executed only when the above function doesn't throw a fatal error
$worddict2=$worddicttemp;
}
catch(Exception $e)
{
continue;
}
}
But I'm still getting the same error.
What is wrong? Why is the code inside myfunc() now transferring control to the catch blocks as soon as it encounters that fatal error?
I propose you to use some beautifier script like Tidy before parsing. And your problem can be solved by adding
$html_content = htmlspecialchars($html_content)
You can't catch Parse Errors (or any Fatal Errors for that matter, but Parse Errors are even worse since they'll be generated as soon as the code is loaded). The best way I know of to isolate them is to run completely independent PHP processes for whatever you want to recover from and expect to generate Fatal Errors.
See also How do I catch a PHP Fatal Error
This is a bit of a long shot, but I figured I'd ask anyway. I have an application that has web-based code editing, like you find on Github, using the ACE editor. The problem is, it is possible to edit code that is within the application itself.
I have managed to detect parse errors before saving the file, which works great, but if the user creates a runtime error, such as MyClass extends NonExistentClass, the file passes the parse check, but saves to the filesystem, killing the application.
Is there anyway to test if the new code will cause a runtime error before I save it to the filesystem? Seems completely counter-intuitive, but I figured I'd ask.
Possibly use register_shutdown_function to build a JSON object containing information about the fatal error. Then use an AJAX call to test the file; parse the returned value from the call to see if there is an error. (Obviously you could also run the PHP file and parse the JSON object without using AJAX, just thinking about what would be the best from a UX standpoint)
function my_shutdown() {
$error = error_get_last();
if( $error['type'] == 1 ) {
echo json_encode($error);
}
}
register_shutdown_function('my_shutdown');
Will output something like
{"type":1,"message":"Fatal error message","line":1}
Prepend that to the beginning of the test file, then:
$.post('/test.php', function(data) {
var json = $.parseJSON(data);
if( json.type == 1 ) {
// Don't allow test file to save?
}
});
Possibly helpful: php -f <file> will return a non-zero exit code if there's a runtime error.
perhaps running the code in a separate file first and attach some fixed code on the bottom to check if it evaluates?
I have a eval function like this
if(FALSE === #eval($code)) echo 'your code has php errors';
So if the code has synthax errors it will return that message.
The problem is that if within the code you have something like:
require_once('missing_file.php');
it will just break the page, without my nice error message :(
Is there any workaround for this?
Well, first I hope that $code comes from a trusted source and that you're executing arbitrary code sent by the users.
Second, the only way I see you can workaround that is to save $code into a file, run it with the command line PHP interpreter, and check the exit value. Note that passing this test doesn't make $code fatal error free, it just so happened that this particular execution of the script did not throw any fatal error; there may be other code paths that trigger such an error.
This is because once eval triggers a fatal error, it can't be recovered and the script dies. eval only returns FALSE if there is a parsing error.