I want to open a Java GUI using another Java program. It successfully compiled, but it can't be run. It says "cannot load main class". Here is my code:
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Test {
public static void main(String[] args) {
try {
JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
jc.run(System.in, System.out, System.err, "src/HelloWorld.java");
// I think this line has the error
Runtime.getRuntime().exec("cmd.exe /c start java src/HelloWorld");
} catch (Exception e) {
e.printStackTrace();
}
}
}
HelloWorld class:
import javax.swing.JFrame;
public class HelloWorld {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(100, 500);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
There are any number of possible places your code might fail, for example, it could fail to compile for some reason; java may not be in the OS's search path; the java command may fail, as the src/HelloWorld parameter looks wrong.
I believe your command should look more like java -cp ./src HelloWorld or it should be executed from within the context of the src directory...
You really need to make every effort to diagnose potential issues, such as the DiagnosticCollector for the JavaCompiler and reading the processes InputStream and ErrorStream
Based on your HelloWorld.java and this previous answer I was able generate this example, which can successfully compile and run your class...
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class TestCompile {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(64);
try {
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
// >>>> Compiler Stage
// This sets up the class path that the compiler will use.
// I've added the .jar file that contains the DoStuff interface within in it...
List<String> optionList = new ArrayList<>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path"));
Iterable<? extends JavaFileObject> compilationUnit
= fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File("src/HelloWorld.java")));
JavaCompiler.CompilationTask task = compiler.getTask(
null,
fileManager,
diagnostics,
optionList,
null,
compilationUnit);
if (task.call()) {
// <<<< Compiler Stage
// >>>> Run stage...
ProcessBuilder pb = new ProcessBuilder("java", "-cp", "./src", "HelloWorld");
pb.redirectError();
Process p = pb.start();
InputStreamConsumer.consume(p.getInputStream());
p.waitFor();
// <<<< Run Stage
} else {
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
System.out.format("Error on line %d in %s%n",
diagnostic.getLineNumber(),
diagnostic.getSource().toUri());
}
}
} catch (InterruptedException ex) {
Logger.getLogger(TestCompile.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
public static class InputStreamConsumer implements Runnable {
private InputStream is;
public InputStreamConsumer(InputStream is) {
this.is = is;
}
public InputStream getInputStream() {
return is;
}
public static void consume(InputStream is) {
InputStreamConsumer consumer = new InputStreamConsumer(is);
Thread t = new Thread(consumer);
t.start();
}
#Override
public void run() {
InputStream is = getInputStream();
int in = -1;
try {
while ((in = is.read()) != -1) {
System.out.print((char)in);
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}
As an aternative to using ProcessBuilder pb = new ProcessBuilder("java", "-cp", "./src", "HelloWorld");, you could actually use...
ProcessBuilder pb = new ProcessBuilder("java", "HelloWorld");
pb.directory(new File("src"));
pb.redirectError();
Process p = pb.start();
Which will launch the java process in within the context of the src directory
Related
Is it possible that to read a .java file as a file (using file path)from another class and call its method in this class ?
Lets takes a java class as
public Mylogic {
public static void test()
{
//some logic
}
}
Is there a way another java class can read Mylogic.java file as a file and execute test() method
?
Why I want this?
Once source code goes into application server , then if I have to add another class , I have to wait for complete deployment which takes time.If I am able to do this, I can keep a utility class ready in source code to read .java file from dir and execute it without any deployment, thus saving time.
This is for higher environment (production mode) so no exploded mode.
It is possible, if you have JDK instead of JRE when running the code, using javax.tools.JavaCompiler and a customized class loader:
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class LoadAndRun extends ClassLoader {
public Class findClass(String name) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
InputStream in = new FileInputStream("./" + name + ".class");
byte [] buff = new byte[4096];
while (true) {
int c = in.read(buff);
if (c == -1) break;
out.write(buff, 0, c);
}
return defineClass(name, out.toByteArray(), 0, out.toByteArray().length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String [] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
// Try to get the system compiler
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
System.err.println("System java compiler not found.");
return;
}
// Prepare the java file to be compiled
String classname = "LoadAndRun" + System.currentTimeMillis();
File javaFile = new File(classname + ".java");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(javaFile)));
writer.write("public class " + classname + "{\n");
writer.write("public static void test() {System.out.println(\"this is the test class:\"+"+classname+".class);}");
writer.write("}\n");
writer.close();
// Compile it!
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(javaFile)).call();
// Load and invoke static method
ClassLoader classLoader = new LoadAndRun();
Class cls = classLoader.loadClass(classname);
Method m = cls.getMethod("test");
m.invoke(null);
// Clean up
m = null;
cls = null;
classLoader = null;
System.gc();
javaFile.delete();
File classFile = new File(classname + ".class");
classFile.delete();
}
}
I want to run 2 cmd commands consecutively. My purpose here, first compile file(with using cmd command not any other things like Java Compiler API), then run.
compiler.java:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class compiler {
public static void main(String[] args) {
final String dosCommand = "cmd /c java -cp ";
final String classname = "example";
final String location = "D:\\";
try {
final Process process2 = Runtime.getRuntime().exec("cmd /k javac D:\\example.java"); //I used /k to remain.
final Process process = Runtime.getRuntime().exec(dosCommand + location + " " + classname);
final InputStream in = process.getInputStream();
final InputStream in2 = process.getErrorStream();
int ch, ch2;
while ((ch = in.read()) != -1) {
System.out.print((char) ch);
}
while ((ch2 = in2.read()) != -1) {
System.out.print((char) ch2); // read error here
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
example.java:(in D:// path.)
public class example {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
When I run compiler.java it gives
Error: Couldn't find or load main class example
No problem in example.java. When I compile and run this example.java file in cmd it runs correctly.
My problem is to run 2 cmd commands consecutively. Finally, How can I run cmd commands consecutively?. Thanks...
I have the following code segment to run a bat file:
String workingDir = System.getProperty("user.dir");
ProcessBuilder pb = new ProcessBuilder("cmd", "/c",
"\"" + workingDir + File.separator + "midl.bat\"");
Process ddsBuildProc = pb.start();
ddsBuildProc.waitFor();
The workingDir includes spaces in the path. Eventhough I use quotes to enclose the workingDir+fileName string, the shell still splits the workingDir and doesn't run the bat file. If a try and copy-paste-execute the bat file path string in the Windows command window manually, it works as expected. What can be the problem here?
Also, please do not close this question as duplicate because I tried all the solutions in the other questions with no success.
Don't quote commands in a command list, unless the command been executed expects it, this will just stuff things up
user.dir is your programs current executing context...so it actually makes no sense to include it, you could just use midl.bat by itself (assuming the command exists within the current execution context)
I wrote a really simple batch file...
#echo off
dir
Which I put in my "C:\Program Files" directory, as I need a path with spaces and used....
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class RunBatch {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder(
"cmd", "/c", "listme.bat"
);
pb.directory(new File("C:/Program Files"));
pb.redirectError();
try {
Process process = pb.start();
InputStreamConsumer.consume(process.getInputStream());
System.out.println("Exited with " + process.waitFor());
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}
public static class InputStreamConsumer implements Runnable {
private InputStream is;
public InputStreamConsumer(InputStream is) {
this.is = is;
}
public static void consume(InputStream inputStream) {
new Thread(new InputStreamConsumer(inputStream)).start();
}
#Override
public void run() {
int in = -1;
try {
while ((in = is.read()) != -1) {
System.out.print((char) in);
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}
To run it without any issues...
I am facing the following problem:
I will receive one or more .java files. The idea is to automate the compile and execute process.
I have not written nor viewed .java source files i receive.
There may be 1 file or multiple files in multiple directories.
All of this is done under linux ( Debian / CentOS ).
Here is an example case:
2 files are received:
SomeFile.java and SomeOtherFile.Java ( This one has the static public void main(String args[]){} method but i do NOT know that !)
A process picks up the files and compiles them in this way:
javac -encoding UTF-8 -sourcepath . -d . *.java
So my problem is: I do not know which package(if any) contains the Main method so i do not know what do execute ?
java packageName.SomeOtherFile
Plenty of ways:
Get a java source code parser, parse the source code, find the method
Compile everything, go over the resulting *.class files using reflection, find the method.
For a small enough number of files, just try them all
I have written something like this before:
package vonbulow.nicki;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.jar.Manifest;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
*
* #author Nicki
*/
public class AppLoader extends ClassLoader {
private static final String userdir = System.getenv("USERPROFILE");
private static final AppLoader instance = new AppLoader();
private static HashMap<String, Class> loaded = new HashMap<String, Class>();
public static void loadapp(final String name) {
if(loaded.containsKey(name)) {
Thread d = new Thread(new Runnable(){
public void run(){
try {
Class c = loaded.get(name);
Method m = c.getDeclaredMethod("main", String[].class);
m.invoke(null, (Object[])new String[]{null});
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
}
}
});
d.start();
return;
}
File ud = new File(userdir+"\\nvbapp");
ud.mkdir();
File[] fa = ud.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".appn");
}
});
for(final File f:fa) {
if(f.getName().split("\\.")[0].equalsIgnoreCase(name)) {
Thread t = new Thread(new Runnable() {
public void run() {
runapp(f, name);
}
});
t.start();
}
}
}
private static void runapp(File f, String nam) {
List<Class> classes = new ArrayList<Class>();
ZipFile jf;
String name = "";
try {
jf = new ZipFile(f);
Enumeration<? extends ZipEntry> eze = jf.entries();
while(eze.hasMoreElements()){
ZipEntry ze = eze.nextElement();
if(ze.getName().endsWith(".class")&&!ze.isDirectory()){
InputStream fis = jf.getInputStream(ze);
byte[] bytes = new byte[(int)ze.getSize()];
fis.read(bytes);
classes.add(instance.defineClass(getClassName(bytes), bytes, 0, bytes.length));
}
if(ze.getName().equalsIgnoreCase("META-INF/MANIFEST.MF")) {
Manifest manifest = new Manifest(jf.getInputStream(ze));
name = manifest.getMainAttributes().getValue("Main-Class");
}
}
Iterator<Class> classit = classes.iterator();
while(classit.hasNext()) {
Class c = classit.next();
if(c.getName().equals(name)) {
try {
loaded.put(nam, c);
Method m = c.getDeclaredMethod("main", String[].class);
m.invoke(null, (Object[]) new String[]{null});
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
ex.printStackTrace();
}
}
}
} catch (IOException ex) {
Logger.getLogger(AppLoader.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static String getClassName(byte[] is) {
try {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(is));
dis.readLong(); // skip header and class version
int cpcnt = (dis.readShort()&0xffff)-1;
int[] classes = new int[cpcnt];
String[] strings = new String[cpcnt];
for(int i=0; i<cpcnt; i++) {
int t = dis.read();
if(t==7) classes[i] = dis.readShort()&0xffff;
else if(t==1) strings[i] = dis.readUTF();
else if(t==5 || t==6) { dis.readLong(); i++; }
else if(t==8) dis.readShort();
else dis.readInt();
}
dis.readShort(); // skip access flags
return strings[classes[(dis.readShort()&0xffff)-1]-1].replace("/", ".");
} catch (IOException ex) {
return null;
}
}
}
I have not edited it; you will need to edit it so it loads your own classes.
You also need to compile the files first with the JavaCompiler class. This also assumes that the classes are in the zip file.
I am trying to develop a script in Java which finds all .jar files in a specified directory, then them to classpath and under certain conditions, invokes their main() method. Here is my Java info:
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~12.04.1)
OpenJDK Server VM (build 20.0-b12, mixed mode)
Here is the ls of the current working dir:
clojure.jar
loader.class
loader.java
I am doing the following in order to add clojure.jar to the classpath and invoke its main method:
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import java.lang.reflect.Method;
public final class loader {
public static void main (String[] args) {
try {
printClasspathString();
System.out.println ("**********");
URL[] classesRepo = { new File("clojure.jar").toURI ().toURL (),
new File(System.getProperty("user.dir")).toURI ().toURL ()};
ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
URLClassLoader urlClassLoader = new URLClassLoader( classesRepo, currentThreadClassLoader);
Thread.currentThread().setContextClassLoader(urlClassLoader);
printClasspathString();
} catch (Exception ex) {
System.out.println(ex.getMessage ());
}
//Do I miss something here?
String mainClassName="clojure.main";
Class<?> mainClass = null;
try {
mainClass = Class.forName(mainClassName);
}
catch (Exception ex) {
throw new IllegalArgumentException("class not found in your jar file " + mainClassName);
}
Method mainMethod = null;
try {
mainMethod = mainClass.getMethod("main", String[].class);
}
catch (Exception ex) {
throw new IllegalArgumentException("class to launch (" + mainClassName + ") does not have a public static void main(String[]) method");
}
try {
mainMethod.invoke(null, (Object) args);
} catch (Exception ex) {
System.out.println(ex.getMessage ());
}
}
public static void printClasspathString() {
ClassLoader applicationClassLoader = Thread.currentThread().getContextClassLoader();
if (applicationClassLoader == null) {
applicationClassLoader = ClassLoader.getSystemClassLoader();
}
URL[] urls = ((URLClassLoader)applicationClassLoader).getURLs();
for(int i=0; i < urls.length; i++) {
System.out.println (urls[i].getFile());
}
}
}
Unfortunately, the loader doesn't work as expected:
$ java -cp . loader
/home/proofit404/data/downloads/clojure-loader/
**********
/home/proofit404/data/downloads/clojure-loader/clojure.jar
/home/proofit404/data/downloads/clojure-loader/
Exception in thread "main" java.lang.IllegalArgumentException: class not found in your jar file clojure.main
at loader.main(loader.java:37)
If I use the -cp option, though, everything works fine:
$ java -cp .:clojure.jar loader
/home/proofit404/data/downloads/clojure-loader/
/home/proofit404/data/downloads/clojure-loader/clojure.jar
**********
/home/proofit404/data/downloads/clojure-loader/clojure.jar
/home/proofit404/data/downloads/clojure-loader/
Clojure 1.4.0
user=> (System/exit 0)
So - what is it that I need to change in my code to make it work as expected?
I think the problem is that the Class.forName(String) method does not use the threads contextclassloader, but the classloader of the current class:
public static Class<?> forName(String className)
throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.
This means your URLClassLoader wont be used. Try instead to explicitly pass the classloader by using Class.forName(String,boolean, ClassLoader):
mainClass = Class.forName(mainClassName, true, urlClassLoader);
Try this code and follow the comments given below:
import java.net.URL;
import java.io.IOException;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
public class JarLoader extends URLClassLoader {
public JarLoader(URL[] urls) {
super(urls);
}
public void addFile(String path) throws MalformedURLException {
String urlPath = "jar:file://" + path + "!/";
addURL(new URL(urlPath));
}
public static void main(String args[]) {
try {
System.out.println("First attempt...");
Class.forName("org.gjt.mm.mysql.Driver");
//specify your class name above
} catch (Exception ex) {
System.out.println("Failed.");
}
try {
URL urls[] = {};
JarLoader cl = new JarLoader(urls);
cl
.addFile("/opt/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar");
// give your jar file above.
System.out.println("Second attempt...");
cl.loadClass("org.gjt.mm.mysql.Driver");
//specify your class name above
System.out.println("Success!");
} catch (Exception ex) {
System.out.println("Failed.");
ex.printStackTrace();
}
}
}