0% found this document useful (0 votes)
62 views2 pages

Java Launcher C Program Code

This document contains source code for a C program that launches Java programs. It checks the number of command line arguments and either runs a Java program specified in a configuration file or copies the launcher program, configures it, and makes it executable to launch another Java program. It uses fork(), execlp(), and wait() functions to launch the Java program in a child process. It also uses fopen(), fread(), fwrite(), fscanf(), fprintf(), and chmod() functions to read from and write to configuration files.

Uploaded by

kpcmj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views2 pages

Java Launcher C Program Code

This document contains source code for a C program that launches Java programs. It checks the number of command line arguments and either runs a Java program specified in a configuration file or copies the launcher program, configures it, and makes it executable to launch another Java program. It uses fork(), execlp(), and wait() functions to launch the Java program in a child process. It also uses fopen(), fread(), fwrite(), fscanf(), fprintf(), and chmod() functions to read from and write to configuration files.

Uploaded by

kpcmj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10.05.2014 main.

c 1
/home/marvin/Workspaces/C++/JavaLauncher/main.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc, char* argv[]){
if(argc <= 1){
FILE *config = fopen("./run", "r");

if(config){
int length = 0;
fscanf(config, "%d", &length);
printf("Length: %d\n", length);

char cmd[length + 1];
cmd[length] = '\0';
fread(cmd, sizeof(char), length, config);


fclose(config);

pid_t child = fork();

if(child == 0){
printf("run java -jar %s:\n\n", cmd);
execlp("java", "java", "-jar", cmd, NULL);
}else if(child > 0){
wait(child);
printf("\n\nEXIT\n");
}else{
perror("Fork Error:");
}

}else{

printf("file not found. cannot run .jar without configuration\n");
printf("run javalauncher <[Link]> <path to [Link]>\n");

}
}else if(argc == 3){
int length_jar = strlen(argv[1]);

char file[strlen(argv[2]) + 3 + 1];
file[strlen(argv[2]) + 3] = '\0';
strcpy(file, argv[2]);

char *run_ext = "run";
strcpy(file + strlen(argv[2]), run_ext);

printf("Write to %s\n", file);

FILE *run = fopen(file, "w");

if(run){

fprintf(run, "%d%s", length_jar, argv[1]);

fclose(run);

}else{
perror("CAnnot open file:");
}

10.05.2014 main.c 2
/home/marvin/Workspaces/C++/JavaLauncher/main.c
printf("Copy launcher %s\n", argv[0]);

FILE *launcher_in = fopen(argv[0], "r");

if(launcher_in){

char fout[strlen(argv[2]) + strlen(argv[1]) + 9 + 1];
fout[strlen(argv[2]) + strlen(argv[1]) + 9] = '\0';

char *postfix = "-Launcher";

strcpy(fout, argv[2]);
strcpy(fout + strlen(argv[2]), argv[1]);
strcpy(fout + strlen(argv[2]) + strlen(argv[1]), postfix);

printf("Write to %s\n", fout);

FILE *launcher_out = fopen(fout, "w");

if(launcher_out){
char buf[50];
size_t read_num = 0;
while((read_num = fread(buf, sizeof(char), 50, launcher_in))){
fwrite(buf, sizeof(char), read_num, launcher_out);
}
fclose(launcher_out);
printf("Make launcher executable\n");
chmod(fout, S_IRWXU | S_IRWXG | S_IXOTH);
printf("All done.\n");
}else{
perror("Cannot open file:");
}

fclose(launcher_in);
}else{
perror("Cannot open file:");
}

}else{
printf("run javalauncher <[Link]> <path to [Link]>\n");
}
return EXIT_SUCCESS;
}

You might also like