1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.internal.util; 18 19import java.lang.reflect.Method; 20 21/** 22 * Binds native framework methods and then invokes a main class with the 23 * remaining arguments. 24 */ 25class WithFramework { 26 27 /** 28 * Invokes main(String[]) method on class in args[0] with args[1..n]. 29 */ 30 public static void main(String[] args) throws Exception { 31 if (args.length == 0) { 32 printUsage(); 33 return; 34 } 35 36 Class<?> mainClass = Class.forName(args[0]); 37 38 System.loadLibrary("android_runtime"); 39 if (registerNatives() < 0) { 40 throw new RuntimeException("Error registering natives."); 41 } 42 43 String[] newArgs = new String[args.length - 1]; 44 System.arraycopy(args, 1, newArgs, 0, newArgs.length); 45 Method mainMethod = mainClass.getMethod("main", String[].class); 46 mainMethod.invoke(null, new Object[] { newArgs }); 47 } 48 49 private static void printUsage() { 50 System.err.println("Usage: dalvikvm " + WithFramework.class.getName() 51 + " [main class] [args]"); 52 } 53 54 /** 55 * Registers native functions. See AndroidRuntime.cpp. 56 */ 57 static native int registerNatives(); 58} 59