-3

I want to create a program that can Compile a .java file to a .class file like it is done on this website: Innovation website

My questions are:

  • Is there an library i can use to do this?
  • Where can I get this Library?
  • If there isn't a library for this then how can i do this?
B001ᛦ
  • 2,036
  • 6
  • 23
  • 31
Fryslan
  • 13
  • 5
  • It is compiling the files. You have to get the files and pass them as arguments to the jdk. I don't think there is a library to do this. This is very straight forward once you know how java works. – luanjot Jan 11 '15 at 13:10
  • @luanjot It's there in JDK since 1.6. – laune Jan 11 '15 at 13:19
  • possible duplicate of [How to run Java program in command prompt](http://stackoverflow.com/questions/11965818/how-to-run-java-program-in-command-prompt) – Coding Enthusiast Jan 11 '15 at 13:25

3 Answers3

5
   JavaCompiler javac = ToolProvider.getSystemJavaCompiler();

The javadoc for JavaCompiler shows how to work and compile with this JDK facility. One snippet from there:

   File[] files1 = ... ; // input for first compilation task

   JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
   StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

   Iterable<? extends JavaFileObject> compilationUnits1 =
       fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files1));
   compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();

It is by no means a simple task. But a general method for invoking a Java compiler via the shell - with all the trimmings - isn't easy either, and this, then, also applies to calls via Process.

laune
  • 31,114
  • 3
  • 29
  • 42
2

You can do this using the Process class as

Process pro=Runtime.getRuntime().exec("javac FileName.java", null, new File("\path"));
Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
Abhi
  • 341
  • 7
  • 15
0

use ProcessBuilder to execute system commands.

Install JDK and form command "javac JavaFileName" pass this to ProcessBuilder constructor and call method start()

Saravana
  • 12,647
  • 2
  • 39
  • 57