Advertisement
Guest User

java-jar-launch.c++

a guest
Nov 11th, 2010
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include<cstdlib>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<string>
  5. #include <unistd.h>
  6.  
  7. using namespace std;
  8.  
  9. void usage()
  10. {
  11.     printf("Usage: java-jar-lauch <your-jar-here>\n");
  12. }
  13.  
  14. void dirUp(char* myDir)
  15. {
  16.     int len = strlen(myDir);
  17.     while (1)
  18.     {
  19.         len--;
  20.         if (len == 0)
  21.         {
  22.             printf("Invalid folder!");
  23.             exit(-1);
  24.         }
  25.         if (myDir[len] == '/')
  26.         {
  27.             myDir[len+1] = '\0';
  28.             return;
  29.         }
  30.     }
  31.  
  32. }
  33.  
  34.  
  35. int main(int argc, char** args)
  36. {
  37.     if (argc == 1)
  38.     {
  39.         usage();
  40.         return 0;
  41.     }
  42.  
  43.     char* workingDir = new char[strlen(args[1])];
  44.     strcpy(workingDir, args[1]);
  45.     dirUp(workingDir);
  46.     printf("CWD: %s\n", workingDir);
  47.     char* cmd = new char[strlen(args[1]) + strlen("java -jar") + 2];
  48.     sprintf(cmd, "java -jar %s", args[1]);
  49.     printf("CMD: %s\n", cmd);
  50.     chdir(workingDir);
  51.     system(cmd);
  52.     delete[] cmd;
  53.     delete[] workingDir;
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement