In C++ you can use int system (const char* command);
function.
Example:
/* system example : DIR */
#include <stdio.h> /* printf */
#include <stdlib.h> /* system, NULL, EXIT_FAILURE */
int main ()
{
int i;
printf ("Checking if processor is available...");
if (system(NULL)) puts ("Ok");
else exit (EXIT_FAILURE);
printf ("Executing command DIR...\n");
i=system ("dir");
printf ("The value returned was: %d.\n",i);
return 0;
}
Source: http://www.cplusplus.com/reference/cstdlib/system/
In your case you can use:
system ("sudo apt-get upgrade");
In Java is slightly more complicated. You can use exec
method of the Runtime class in java.lang. See http://www.linuxforums.org/forum/programming-scripting/65117-c-c-system-function-analog-java.html