Register FAQ/Rules Forum Spy Search Today's Posts Mark Forums Read

Welcome to the Mac Forums forums. Please read the FAQ if you have questions. Register to participate.

 
Go Back   Mac Forums > Apple Software > Mac Programming
TouchArcade.com - iPhone Game Reviews and News

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 11, 2006, 07:19 AM   #1
Eraserhead
Contributor
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: Oxford, UK
Run Applescript from java

I am trying to run the following code, when the RunAppleScript method is launched it comes up with the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/apple/cocoa/foundation/NSAppleScript
at Copying.RunApplescript(Copying.java:141)
at Copying.main(Copying.java:161)
line 141 is 'NSAppleScript myScript =new NSAppleScript(script);'

Code:
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
public class test{
	public static void RunApplescript(String script){
		// This creates a new NSAppleScript
		// object to execute the script
		NSAppleScript myScript =new NSAppleScript(script);
		
		// This dictionary holds any errors that are
		// encountered during script execution
		NSMutableDictionary errors =
			new NSMutableDictionary();
		
		// Execute the script!
		NSAppleEventDescriptor results=myScript.execute(errors);
	}
}
What's the problem?
Eraserhead is offline   Reply With Quote
Old Apr 11, 2006, 10:44 AM   #2
jeremy.king
macrumors 601
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
Quote:
Originally Posted by Eraserhead
What's the problem?
Is /System/Library/Java in the java classpath?
jeremy.king is offline   Reply With Quote
Old Apr 11, 2006, 11:04 AM   #3
Eraserhead
Thread Starter
Contributor
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: Oxford, UK
Quote:
Originally Posted by kingjr3
Is /System/Library/Java in the java classpath?
I have tried adding

I have typed this into Terminal
'javac -classpath "/System/Library/Java/:." Test.java' and the same error occurs.

As I am using XCode I have also tried putting '-classpath /System/Libray/Java/' in the Build Tab of the inspector for Test.java

I don't know if I'm doing it correctly...
Eraserhead is offline   Reply With Quote
Old Apr 11, 2006, 11:29 AM   #4
jeremy.king
macrumors 601
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
Quote:
Originally Posted by Eraserhead
I have tried adding

I have typed this into Terminal
'javac -classpath "/System/Library/Java/:." Test.java' and the same error occurs.

As I am using XCode I have also tried putting '-classpath /System/Libray/Java/' in the Build Tab of the inspector for Test.java

I don't know if I'm doing it correctly...
I can't speak to XCode, but I'm not sure you need the trailing slash in your javac command classpath.

guess I could try really quick.
jeremy.king is offline   Reply With Quote
Old Apr 11, 2006, 11:32 AM   #5
jeremy.king
macrumors 601
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
Quote:
Originally Posted by Eraserhead
I have typed this into Terminal
'javac -classpath "/System/Library/Java/:." Test.java' and the same error occurs.
Also make sure your class name and your file name are the SAME, case included. In your code example, your filename should be test.java not Test.java since the class name is test

You also don't have a main method, make sure you include one in your class.
jeremy.king is offline   Reply With Quote
Old Apr 11, 2006, 11:51 AM   #6
Eraserhead
Thread Starter
Contributor
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: Oxford, UK
Quote:
Originally Posted by kingjr3
Also make sure your class name and your file name are the SAME, case included. In your code example, your filename should be test.java not Test.java since the class name is test

You also don't have a main method, make sure you include one in your class.
My class has a main method, sorry I should have included it , the code is

Code:
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;

public class Test {
	public static void RunApplescript(String script){
		// This creates a new NSAppleScript
		// object to execute the script
		NSAppleScript myScript =new NSAppleScript(script);
		
		// This dictionary holds any errors that are
		// encountered during script execution
		NSMutableDictionary errors =
			new NSMutableDictionary();
		
		// Execute the script!
		NSAppleEventDescriptor results=myScript.execute(errors);
	}
	public static void main (String args[]) {
		String cmd="tell application \"Finder\" \n"+"make new folder in folder \"Matthew's HD\" with properties{name:\"testing\"} \n"+"end tell";
		RunApplescript(cmd);
	}
}
the class and the file are both called Test.java, and I use 'javac -classpath "/System/Library/Java:." Test.java' to compile it in the command line.
Eraserhead is offline   Reply With Quote
Old Apr 11, 2006, 12:16 PM   #7
jeremy.king
macrumors 601
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
don't forget the -classpath on your java command too.

this worked fine for me.
Code:
javac -classpath /System/Library/Java Test.java
sudo  java -classpath "/System/Library/Java:." Test
You'll need sudo for access to the Window Server to create the folder
jeremy.king is offline   Reply With Quote

Reply

Mac Forums > Apple Software > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 01:57 PM.

Mac News | Mac Rumors | iPhone Game Reviews | iPhone Apps

Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 2002-2009, MacRumors.com, LLC