aspose file tools*
The moose likes I/O and Streams and the fly likes redirect .class files programatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "redirect .class files programatically" Watch "redirect .class files programatically" New topic
Author

redirect .class files programatically

Amir Iqbal
Ranch Hand

Joined: Jun 23, 2007
Posts: 97
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.


i am Java +ve Now !
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Amir Iqbal
Ranch Hand

Joined: Jun 23, 2007
Posts: 97
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

Programatically? How are you running the compiler, exactly -- do you mean using the JDK 1.6 JavaCompiler class?
Amir Iqbal
Ranch Hand

Joined: Jun 23, 2007
Posts: 97
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

Joined: Oct 27, 2005
Posts: 19755
    
  20

From reading the API, this is what I would try next:

Not tested though.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: redirect .class files programatically