souparno majumder
souparno majumder

Reputation: 2052

Is it possible to compile and run java without using any shell?

This question just popped to my mind. To compile java, we need to use the $JAVA_HOME/bin/javac file and pass to it the java files that needs to be compiled. I was wondering if it is possible to execute javac file without using the bash/shell. Or use any other programming languages to execute the javac file directly?

Upvotes: 1

Views: 596

Answers (2)

Anonymous Coward
Anonymous Coward

Reputation: 3200

Yes, it is possible.

#include <unistd.h>
int main(void)
{
  execl( "/path/javac","javac", "/path/to/program.java", NULL );
  return 0;
}

That C program will execute the compiler and compile program.java without using a shell. Just run it also without using a shell and you have run the compiler without a shell. It is not a very useful program, it always compiles the same single file. But it is possible to modify it to read a file with a list of files to compile. Which is actually what IDEs do. And that is another way of running javac without a shell.

As for running the compiled java program the same principle applies. Create a program which runs the java interpreter.

Upvotes: 3

user1886323
user1886323

Reputation: 1199

It is not possible to use javac without, on some level, executing it as a shell command, because by its nature it is a command line program. This means that it can be executed from any programming language that can execute native shell commands.

What most people do after learning how javac works by executing it manually from a shell is configure their IDE to execute javac with the correct arguments when developing, and configure a build technology such as maven or ant to execute javac to produce their deployable application package.

Upvotes: -2

Related Questions