0

I have an issue for my application.

I want to convert selected .java file to .class file at same path.

for that i am using:

   File directoryPath = new File(".");
    String command ="cmd.exe /C "+ directoryPath.getCanonicalPath()+"\\javac UpsOfferDataDaily.java"; 
                Runtime.getRuntime().exec(command);

any thing strange with that??

It Can't execute my command successfully.

directory path will be same as the .java is situated.

what should i do...

Thanks in advance:

Siten
  • 4,515
  • 9
  • 39
  • 64

2 Answers2

2

Try this....javac is already in my path. So i jus gave the filename. Errors will be shown by the error stream, if any...It worked for me!!!

String command ="cmd.exe /C "+ "javac C:\\student\\workspace\\javaproject\\Testing\\src\\TestCalculator.java"; 
        Process p = Runtime.getRuntime().exec(command);
        InputStream i = p.getErrorStream();
        int c;
        while((c=i.read())!=-1)
            System.out.print((char)c);
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
  • I am getting error like "javac' is not recognized as an internal or external command,operable program or batch file."How to set the path bydefault in cmd when this command excute. – Siten Feb 18 '12 at 09:54
  • You set it externally...set the environmental variable "path"...right computer > Properties >Advanced tab > Environmental variables – Shashank Kadne Feb 18 '12 at 09:57
  • No...you still got the error??...open cmd from outside...and type javac and see if it recognizes the command..... – Shashank Kadne Feb 18 '12 at 10:05
  • i set path environment till/bin. java command is working but javac is not working and after that i manually set the java path till /bin then javac command is running. after that i run your code it give same error. i am hang... – Siten Feb 18 '12 at 10:12
  • No manual setting wud be limited to that prompt itself....did you make sure javac file exist inside your bin....if java is working directly den javac shud work too... – Shashank Kadne Feb 18 '12 at 10:15
2

You could use the built-in api; javax.tool.JavaCompiler.

http://docs.oracle.com/javase/7/docs/api/javax/tools/JavaCompiler.html

http://www.javabeat.net/articles/73-the-java-60-compiler-api-1.html

KarlP
  • 5,149
  • 2
  • 28
  • 41