Skip to content

Instantly share code, notes, and snippets.

Created February 19, 2012 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1865926 to your computer and use it in GitHub Desktop.
Save anonymous/1865926 to your computer and use it in GitHub Desktop.
Simplified JavaAppLauncher
defaults write full.qualified.bundle.id JVMDefaults -array '-Xmx2048m' '-Xms128m'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleName</key>
<string>AppName</string>
<key>CFBundleIdentifier</key>
<string>full.qualified.bundle.id</string>
<key>CFBundleExecutable</key>
<string>JavaAppLauncher</string>
<key>JavaArguments</key>
<array>
<string>-cp</string>
<string>$ROOT/lib/*:$ROOT/Classes:$ROOT/PlugIns/jdk/Contents/Home/lib/tools.jar</string>
<string>-Djava.library.path=$ROOT/dylib</string>
<string>-ea</string>
<string>-esa</string>
<string>-DsomeSysproperty</string>
<string>name.of.main.Class</string>
<string>main_arg0</string>
<string>main_arg1</string>
</array>
</dict>
</plist>
/*
* Copyright 2012, Oracle and/or its affiliates. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#import <Cocoa/Cocoa.h>
#include <jni.h>
#define JAVA_LAUNCH_ERROR "JavaLaunchError"
#define JAVA_ARGUMENTS_KEY "JavaArguments"
// TODO Remove these; they are defined by the makefile
#define FULL_VERSION "1.7.0"
#define DOT_VERSION "1.7.0"
#define DEFAULT_POLICY 0
typedef int (JNICALL *JLI_Launch_t)(int argc, char ** argv,
int jargc, const char** jargv,
int appclassc, const char** appclassv,
const char* fullversion,
const char* dotversion,
const char* pname,
const char* lname,
jboolean javaargs,
jboolean cpwildcard,
jboolean javaw,
jint ergo);
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int result;
@try {
// Get the main bundle
NSBundle *mainBundle = [NSBundle mainBundle];
// Get the arguments
NSDictionary *infoDictionary = [mainBundle infoDictionary];
NSArray *arguments = [infoDictionary objectForKey:@JAVA_ARGUMENTS_KEY];
if (arguments == nil) {
[NSException raise:@JAVA_LAUNCH_ERROR format:@"%@ is required.", @JAVA_ARGUMENTS_KEY];
}
// Get the defaults
NSArray *defaults = [[NSUserDefaults standardUserDefaults] arrayForKey:@"JVMDefaults"];
if (defaults == nil) {
defaults = [ NSArray array ];
}
// Load the runtime bundle which has to be named jdk and reside in PlugIns directory
NSURL *runtimeBundleURL = [[mainBundle builtInPlugInsURL] URLByAppendingPathComponent:@"jdk"];
CFBundleRef runtimeBundle = CFBundleCreate(NULL, (CFURLRef)runtimeBundleURL);
NSError *bundleLoadError = nil;
Boolean runtimeBundleLoaded = CFBundleLoadExecutableAndReturnError(runtimeBundle, (CFErrorRef *)&bundleLoadError);
if (bundleLoadError != nil || !runtimeBundleLoaded) {
[NSException raise:@JAVA_LAUNCH_ERROR format:@"Could not load JRE from %@.", bundleLoadError];
}
// Get the JLI_Launch() function pointer
JLI_Launch_t JLI_LaunchFxnPtr = CFBundleGetFunctionPointerForName(runtimeBundle, CFSTR("JLI_Launch"));
if (JLI_LaunchFxnPtr == NULL) {
[NSException raise:@JAVA_LAUNCH_ERROR format:@"Could not get function pointer for JLI_Launch."];
}
// Initialize the arguments to JLI_Launch() replacing $ROOT by bundle path
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<!\\\\)\\$ROOT(?!\\w)" options:0 error:&error];
NSString *bundlePath = [[mainBundle bundlePath] stringByAppendingString:@"/Contents"];
int i = 0;
int jargc = 1 + [defaults count] + [arguments count];
char *jargv[jargc];
jargv[i++] = strdup(argv[0]);
for (NSString *argument in defaults) {
NSString *arg = [regex stringByReplacingMatchesInString:argument options:0 range:NSMakeRange(0, [argument length]) withTemplate:bundlePath];
jargv[i++] = strdup([arg UTF8String]);
}
for (NSString *argument in arguments) {
NSString *arg = [regex stringByReplacingMatchesInString:argument options:0 range:NSMakeRange(0, [argument length]) withTemplate:bundlePath];
jargv[i++] = strdup([arg UTF8String]);
}
// Invoke JLI_Launch()
JLI_LaunchFxnPtr(jargc, jargv, 0, NULL, 0, NULL, FULL_VERSION, DOT_VERSION, "java", "java", FALSE, TRUE, FALSE, DEFAULT_POLICY);
result = 0;
} @catch (NSException *exception) {
NSLog(@"%@: %@", exception, [exception callStackSymbols]);
result = 1;
}
[pool drain];
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment