Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

redirect .class files programatically

 
Amir Iqbal
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell me that how can i assure that .class files have been generated.....
also how can i redirect the path of .class file programatically.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking how to tell the compiler where to put .class files? That's the "-d" switch to javac. If you're asking something else, please explain more clearly.
 
Amir Iqbal
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i am asking to tell the compiler to place .class files at specific location .... you told that is can be done by "-d" but how can i do this programatically...???

by the way thanks for paying attention
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programatically? How are you running the compiler, exactly -- do you mean using the JDK 1.6 JavaCompiler class?
 
Amir Iqbal
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes my friend i am using JCompiler class..... i use the follwing code it works but unfortunately it donot creats .class file of class HelloWorld but variable sucess is true that shows compilation has done successfully ..... so what do i do to create .class files at specific location say at C:/aamir

this is the code i am using.....

/*
* CompileSourceInMemory.java
*
* Created on March 1, 2008, 8:02 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author aamir
*/
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject.Kind;
import javax.tools.*;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import java.net.*;


public class CompileSourceInMemory {

/** Creates a new instance of CompileSourceInMemory */
public CompileSourceInMemory() {

}
public static void main(String args[]) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.println(\"hey man altaf hussain\");");
out.println(" }");
out.println("}");
out.close();
JavaFileObject file = new JavaSourceFromString("HelloWorld",writer.toString());

Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList (file);
CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);

boolean success = task.call();
System.out.println("me b4 loop");
/*
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
System.out.println("getCode()"+diagnostic.getCode());
System.out.println("getKind()"+diagnostic.getKind());
System.out.println("getPosition()"+diagnostic.getPosition());
System.out.println("getStartPosiotion()"+diagnostic.getStartPosition());
System.out.println("getEndPosition()"+diagnostic.getEndPosition());
System.out.println("getSource()"+diagnostic.getSource());
System.out.println("getMessage()"+diagnostic.getMessage(null));

}*/
System.out.println("Success: " + success);

if (success) {
try {
Class.forName("HelloWorld").getDeclaredMethod("main", new Class[] { String[].class })
.invoke(null, new Object[] { null });
} catch (ClassNotFoundException e) {
System.err.println("Class not found: " + e);
} catch (NoSuchMethodException e) {
System.err.println("No such method: " + e);
} catch (IllegalAccessException e) {
System.err.println("Illegal access: " + e);
} catch (InvocationTargetException e) {
System.err.println("Invocation target: " + e);
}
}
}
}

class JavaSourceFromString extends SimpleJavaFileObject {
final String code;

JavaSourceFromString(String name, String code) {
//JavaFileObject file = new JavaSourceFromString("HelloWorld",writer.toString());
super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension),Kind.SOURCE);
this.code = code;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}




again thanks alot for paying attention to my problem man .
Regards: Amir
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From reading the API, this is what I would try next:

Not tested though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic