Skip to content

Instantly share code, notes, and snippets.

@jiridanek
Created November 4, 2011 19:32
Show Gist options
  • Save jiridanek/1340267 to your computer and use it in GitHub Desktop.
Save jiridanek/1340267 to your computer and use it in GitHub Desktop.
LoadClassJava
[jirka@private result]$ ls
ExecutableClass.class Main.class Spoustec.py Spoustec.py~ TestovaciA.class TestovaciB.class
[jirka@private result]$ python3 Spoustec.py
fred
No such class Exception
TestovaciA
Hello from TestovaciA
TestovaciB
Hello from TestovaciB
asdf
No such class Exception
#### Upravená verze co nahrává pomocí reflexe a volá metodu main ####
[jirka@private result]$ tree
.
├── cz
│   ├── muni
│   │   ├── fi
│   │   │   ├── package.bluej
│   │   │   └── pb162
│   │   │   ├── package.bluej
│   │   │   └── task2
│   │   │   ├── BatteryPowered.class
│   │   │   ├── BatteryPowered.ctxt
│   │   │   ├── BatteryPowered.java
│   │   │   ├── Candle.class
│   │   │   ├── Candle.ctxt
│   │   │   ├── Candle.java
│   │   │   ├── Demo.class
│   │   │   ├── Demo.ctxt
│   │   │   ├── Demo.java
│   │   │   ├── Light.class
│   │   │   ├── Light.ctxt
│   │   │   ├── Light.java
│   │   │   ├── NightWatchman.class
│   │   │   ├── NightWatchman.ctxt
│   │   │   ├── NightWatchman.java
│   │   │   ├── package.bluej
│   │   │   ├── Torch.class
│   │   │   ├── Torch.ctxt
│   │   │   └── Torch.java
│   │   └── package.bluej
│   └── package.bluej
├── ExecutableClass.class
├── Main.class
├── Spoustec.py
├── Spoustec.py~
├── TestovaciA.class
├── TestovaciB.class
└── TestovaciC.class
5 directories, 30 files
[jirka@private result]$ python3 Spoustec.py
cz.muni.fi.pb162.task2.Demo
Night-watchman's equipment: None
Night-watchman's equipment: Candle with beam distance of 3 m
Night-watchman's equipment: Candle with beam distance of 5 m
Night-watchman's equipment: Torch with batteries. Beam distance: 10 m.
Night-watchman's equipment: Torch without batteries. Beam distance: 0 m.
TestovaciC
Hello from TestovaciC
this file is empty
/**
* Created by IntelliJ IDEA.
* User: jirka
* Date: 11/4/11
* Time: 7:16 PM
* To change this template use File | Settings | File Templates.
*/
public interface ExecutableClass {
public void execute();
}
import javax.tools.JavaCompiler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
* User: jirka
* Date: 11/4/11
* Time: 6:59 PM
* To change this template use File | Settings | File Templates.
*/
public class Main {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException {
Scanner s = new Scanner(System.in);
ClassLoader classLoader = Main.class.getClassLoader();
do {
if (! s.hasNextLine()) {
return;
}
String input = s.nextLine();
if (input.equals("")) {
return;
}
Class newClass = classLoader.loadClass(input);
/** pokud můžu přidat rozhraní */
// ExecutableClass e = (ExecutableClass) newClass.newInstance();
// e.execute();
/** pokud ne */
/** kompilátor mi dá poznámku, že třída používá "unsafe operations, tak možná to dělám špatně,
* ale funguje to ;) */
Method method = newClass.getMethod("main", new Class[] {String[].class});
Object e = null; //(ExecutableClass) newClass.newInstance();
// protože http://www.javaworld.com/javaworld/javaqa/1999-07/06-qa-invoke.html
method.invoke(e, new Object[] { new String[]{"arg1", "arg2"}});
} while (true);
}
}
__author__ = 'jirka'
import subprocess
p = subprocess.Popen(shell=True, args='java Main', stdin=subprocess.PIPE)
stdin = p.stdin
while True:
i = input()
stdin.write( (i + '\n').encode() );
#stdin.flush()
if i == '':
break;
#p.wait()
/**
* Created by IntelliJ IDEA.
* User: jirka
* Date: 11/4/11
* Time: 7:18 PM
* To change this template use File | Settings | File Templates.
*/
public class TestovaciA implements ExecutableClass {
public void execute() {
System.out.println("Hello from TestovaciA");
}
}
/**
* Created by IntelliJ IDEA.
* User: jirka
* Date: 11/4/11
* Time: 7:19 PM
* To change this template use File | Settings | File Templates.
*/
public class TestovaciB implements ExecutableClass {
public void execute() {
System.out.println("Hello from TestovaciB");
}
}
/**
* Created by IntelliJ IDEA.
* User: jirka
* Date: 11/4/11
* Time: 8:10 PM
* To change this template use File | Settings | File Templates.
*/
public class TestovaciC {
public static void main(String[] args) {
System.out.println("Hello from TestovaciC");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment