Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Objective-C and Swift
  4. Start java program from Objective C code

Start java program from Objective C code

Scheduled Pinned Locked Moved Objective-C and Swift
helpjavalinuxquestion
10 Posts 4 Posters 66 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DraqueD
    wrote on last edited by
    #1

    I'm trying to make a pretty simple frontend program that's able to start up a java program when it's run, but haven't yet mastered OC. Someone suggested that I try using a console command (code below), but I haven't been able to make that work yet. Is there an easier way to do this, or does any one here know where my code has gone wrong?

    int main(int argc, const char * argv[]) {

    NSFileManager \*filemgr;
    NSString \*currentpath;
    
    filemgr = \[\[NSFileManager alloc\] init\];
    
    currentpath = \[filemgr currentDirectoryPath\];
    
    NSPipe \*pipe = \[NSPipe pipe\];
    NSFileHandle \*file = pipe.fileHandleForReading;
    
    NSTask \*task = \[\[NSTask alloc\] init\];
    task.launchPath = \[currentpath stringByAppendingString:@"/java"\];
    task.arguments = @\[@"-jar", @"PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\];
    task.standardOutput = pipe;
    
    \[task launch\];
    
    NSData \*data = \[file readDataToEndOfFile\];
    \[file closeFile\];
    
    NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\];
    NSLog (@"grep returned:\\n%@", grepOutput);
    
    return 0;
    

    }

    When I run this, I get the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' I know that the current path is accessible, though... so I'm kind of stumped here. Any help would be much appreciated. Thanks!

    J L S 3 Replies Last reply
    0
    • D DraqueD

      I'm trying to make a pretty simple frontend program that's able to start up a java program when it's run, but haven't yet mastered OC. Someone suggested that I try using a console command (code below), but I haven't been able to make that work yet. Is there an easier way to do this, or does any one here know where my code has gone wrong?

      int main(int argc, const char * argv[]) {

      NSFileManager \*filemgr;
      NSString \*currentpath;
      
      filemgr = \[\[NSFileManager alloc\] init\];
      
      currentpath = \[filemgr currentDirectoryPath\];
      
      NSPipe \*pipe = \[NSPipe pipe\];
      NSFileHandle \*file = pipe.fileHandleForReading;
      
      NSTask \*task = \[\[NSTask alloc\] init\];
      task.launchPath = \[currentpath stringByAppendingString:@"/java"\];
      task.arguments = @\[@"-jar", @"PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\];
      task.standardOutput = pipe;
      
      \[task launch\];
      
      NSData \*data = \[file readDataToEndOfFile\];
      \[file closeFile\];
      
      NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\];
      NSLog (@"grep returned:\\n%@", grepOutput);
      
      return 0;
      

      }

      When I run this, I get the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' I know that the current path is accessible, though... so I'm kind of stumped here. Any help would be much appreciated. Thanks!

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      As a guess because what you think is the current path for the executable is not in fact the path where the jar actually is.

      D 1 Reply Last reply
      0
      • J jschell

        As a guess because what you think is the current path for the executable is not in fact the path where the jar actually is.

        D Offline
        D Offline
        DraqueD
        wrote on last edited by
        #3

        Part of the trouble I'm having here is that I'm ultimately trying to reproduce the Terminal line: java -jar PolyGlot.jar <FILENAME_ARGUMENT > Do you have any ideas for that? Any help would be appreciated.

        1 Reply Last reply
        0
        • D DraqueD

          I'm trying to make a pretty simple frontend program that's able to start up a java program when it's run, but haven't yet mastered OC. Someone suggested that I try using a console command (code below), but I haven't been able to make that work yet. Is there an easier way to do this, or does any one here know where my code has gone wrong?

          int main(int argc, const char * argv[]) {

          NSFileManager \*filemgr;
          NSString \*currentpath;
          
          filemgr = \[\[NSFileManager alloc\] init\];
          
          currentpath = \[filemgr currentDirectoryPath\];
          
          NSPipe \*pipe = \[NSPipe pipe\];
          NSFileHandle \*file = pipe.fileHandleForReading;
          
          NSTask \*task = \[\[NSTask alloc\] init\];
          task.launchPath = \[currentpath stringByAppendingString:@"/java"\];
          task.arguments = @\[@"-jar", @"PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\];
          task.standardOutput = pipe;
          
          \[task launch\];
          
          NSData \*data = \[file readDataToEndOfFile\];
          \[file closeFile\];
          
          NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\];
          NSLog (@"grep returned:\\n%@", grepOutput);
          
          return 0;
          

          }

          When I run this, I get the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' I know that the current path is accessible, though... so I'm kind of stumped here. Any help would be much appreciated. Thanks!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          task.launchPath = [currentpath stringByAppendingString:@"/java"];

          Are you sure that the java executable is in that path? Printing out the full path at that point will tell you.

          D 1 Reply Last reply
          0
          • L Lost User

            task.launchPath = [currentpath stringByAppendingString:@"/java"];

            Are you sure that the java executable is in that path? Printing out the full path at that point will tell you.

            D Offline
            D Offline
            DraqueD
            wrote on last edited by
            #5

            Ah, I do see what I was doing there! I was fundamentally misunderstanding how java was being referenced. I almost have it working at this point! Here's the code as it stands now:

            int main(int argc, const char * argv[]) {
            NSFileManager *filemgr;
            NSString *currentpath;

            filemgr = \[\[NSFileManager alloc\] init\];
            
            currentpath = \[filemgr currentDirectoryPath\];
            
            NSPipe \*pipe = \[NSPipe pipe\];
            NSFileHandle \*file = pipe.fileHandleForReading;
            
            NSTask \*task = \[\[NSTask alloc\] init\];
            task.launchPath = @"/usr/bin/java";
            task.arguments = @\[@"-jar", \[currentpath stringByAppendingString:@"/PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\];
            task.standardOutput = pipe;
            
            \[task launch\];
            
            NSData \*data = \[file readDataToEndOfFile\];
            \[file closeFile\];
            
            NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\];
            NSLog (@"grep returned:\\n%@", grepOutput);
            
            return 0;
            

            }

            The only remaining problem is that the "-jar" argument seems to be ignored at this point. If I apply it by itself, Java recognizes it as an argument, but otherwise, it is only accepting "PolyGlot.jar". Is there something that I'm not doing correctly there? Thanks for the help so far!

            L 1 Reply Last reply
            0
            • D DraqueD

              Ah, I do see what I was doing there! I was fundamentally misunderstanding how java was being referenced. I almost have it working at this point! Here's the code as it stands now:

              int main(int argc, const char * argv[]) {
              NSFileManager *filemgr;
              NSString *currentpath;

              filemgr = \[\[NSFileManager alloc\] init\];
              
              currentpath = \[filemgr currentDirectoryPath\];
              
              NSPipe \*pipe = \[NSPipe pipe\];
              NSFileHandle \*file = pipe.fileHandleForReading;
              
              NSTask \*task = \[\[NSTask alloc\] init\];
              task.launchPath = @"/usr/bin/java";
              task.arguments = @\[@"-jar", \[currentpath stringByAppendingString:@"/PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\];
              task.standardOutput = pipe;
              
              \[task launch\];
              
              NSData \*data = \[file readDataToEndOfFile\];
              \[file closeFile\];
              
              NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\];
              NSLog (@"grep returned:\\n%@", grepOutput);
              
              return 0;
              

              }

              The only remaining problem is that the "-jar" argument seems to be ignored at this point. If I apply it by itself, Java recognizes it as an argument, but otherwise, it is only accepting "PolyGlot.jar". Is there something that I'm not doing correctly there? Thanks for the help so far!

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I don't know Objective-C, but I suspect that the first argument should be the same as task.launchPath, i.e the program name. That would then comply with the format of the argv array, as set by the shell when invoking programs from the command line.

              D 2 Replies Last reply
              0
              • L Lost User

                I don't know Objective-C, but I suspect that the first argument should be the same as task.launchPath, i.e the program name. That would then comply with the format of the argv array, as set by the shell when invoking programs from the command line.

                D Offline
                D Offline
                DraqueD
                wrote on last edited by
                #7

                The task.launchPath is the full path of the program to be run. Out of curiosity, I tried dropping " -jar" onto the end, but that made it fail when it looked for the executable "java -jar" rather than the executable "java" with the argument "-jar". What's strange is that the "-jar" argument works correctly so long as it's the only one... Ultimately, the error I'm getting from Java at this point is "

                Could not find or load main class

                ". This typically appears if you try to run a .jar file without the -jar argument. Also, just for the sake of sanity, I'll specify that I can run "java -jar PolyGlot <FILENAME>" from the console, and it runs fine.

                L 1 Reply Last reply
                0
                • D DraqueD

                  The task.launchPath is the full path of the program to be run. Out of curiosity, I tried dropping " -jar" onto the end, but that made it fail when it looked for the executable "java -jar" rather than the executable "java" with the argument "-jar". What's strange is that the "-jar" argument works correctly so long as it's the only one... Ultimately, the error I'm getting from Java at this point is "

                  Could not find or load main class

                  ". This typically appears if you try to run a .jar file without the -jar argument. Also, just for the sake of sanity, I'll specify that I can run "java -jar PolyGlot <FILENAME>" from the console, and it runs fine.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  DraqueD wrote:

                  I can run "java -jar PolyGlot <FILENAME>" from the console, and it runs fine.

                  And I think those are the fields that you need in your arguments array. The last parameter you specify should be argv[1] as a string (%s), not a character (%c), as that is the filename passed in to your program. Something like:

                  task.launchPath = @"/usr/bin/java";
                  task.arguments = @\[task.launchPath, @"-jar", \[currentpath stringByAppendingString:@"/PolyGlot.jar"\], \[NSString stringWithFormat:@"%s", argv\[1\]\]\];
                  
                  1 Reply Last reply
                  0
                  • L Lost User

                    I don't know Objective-C, but I suspect that the first argument should be the same as task.launchPath, i.e the program name. That would then comply with the format of the argv array, as set by the shell when invoking programs from the command line.

                    D Offline
                    D Offline
                    DraqueD
                    wrote on last edited by
                    #9

                    As a heads up, I found my solution! It turns out that the debug folder that this was running in gave Java particular troubles, and the behavior cleared up when I took my compiled app into a typical folder. Thanks to everyone who replied for the help!

                    1 Reply Last reply
                    0
                    • D DraqueD

                      I'm trying to make a pretty simple frontend program that's able to start up a java program when it's run, but haven't yet mastered OC. Someone suggested that I try using a console command (code below), but I haven't been able to make that work yet. Is there an easier way to do this, or does any one here know where my code has gone wrong?

                      int main(int argc, const char * argv[]) {

                      NSFileManager \*filemgr;
                      NSString \*currentpath;
                      
                      filemgr = \[\[NSFileManager alloc\] init\];
                      
                      currentpath = \[filemgr currentDirectoryPath\];
                      
                      NSPipe \*pipe = \[NSPipe pipe\];
                      NSFileHandle \*file = pipe.fileHandleForReading;
                      
                      NSTask \*task = \[\[NSTask alloc\] init\];
                      task.launchPath = \[currentpath stringByAppendingString:@"/java"\];
                      task.arguments = @\[@"-jar", @"PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\];
                      task.standardOutput = pipe;
                      
                      \[task launch\];
                      
                      NSData \*data = \[file readDataToEndOfFile\];
                      \[file closeFile\];
                      
                      NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\];
                      NSLog (@"grep returned:\\n%@", grepOutput);
                      
                      return 0;
                      

                      }

                      When I run this, I get the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' I know that the current path is accessible, though... so I'm kind of stumped here. Any help would be much appreciated. Thanks!

                      S Offline
                      S Offline
                      seemajoshii
                      wrote on last edited by
                      #10

                      I think there is an problem in executable path..Check out that

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups