Thursday, April 25, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1162  / 1 Year ago, tue, january 17, 2023, 1:09:16

How to create command line application c++ or Java?
I have some commands which I would like to convert into an application.

How to do it in eclipse?

Just for example consider:



sudo apt-get upgrade xyz


I want to get it into ubuntu repository.
Or I'll create a gui using qt with c++ or java plugin.


More From » command-line

 Answers
1

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...
");
i=system ("dir");
printf ("The value returned was: %d.
",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


[#29899] Wednesday, January 18, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
feeous

Total Points: 102
Total Questions: 122
Total Answers: 119

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
;