1 #!/usr/bin/env python 2 3 """ 4 A program to run Java class files. 5 6 Copyright (C) 2004, 2005, 2006, 2011 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU Lesser General Public License as published by the Free 10 Software Foundation; either version 3 of the License, or (at your option) any 11 later version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 details. 17 18 You should have received a copy of the GNU Lesser General Public License along 19 with this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 import javaclass.classhook 23 import java.lang 24 25 # NOTE: Simple __this__ package loader to potentially avoid repeated import 26 # NOTE: issues exposed by test.py. 27 28 def load_classes(class_names): 29 30 "Load the classes with the given 'class_names'." 31 32 module = __import__("__this__", globals(), locals(), class_names) 33 objs = [] 34 for class_name in class_names: 35 objs.append(getattr(module, class_name)) 36 return objs 37 38 # The more general class loader. 39 40 def load_class(class_name): 41 42 "Load the class with the given 'class_name'." 43 44 class_name_parts = class_name.split(".") 45 if len(class_name_parts) == 1: 46 module = __import__("__this__", globals(), locals(), [class_name]) 47 obj = getattr(module, class_name) 48 else: 49 class_module = ".".join(class_name_parts[:-1]) 50 obj = __import__(class_module, globals(), locals()) 51 for part in class_name_parts[1:]: 52 obj = getattr(obj, part) 53 54 return obj 55 56 def run_class(cls, args): 57 cls.main([java.lang.String(arg) for arg in args]) 58 59 if __name__ == "__main__": 60 import sys 61 cls = load_class(sys.argv[1]) 62 run_class(cls, sys.argv[2:]) 63 64 # vim: tabstop=4 expandtab shiftwidth=4