Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 11, 2006, 06:19 AM   #1
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: 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   0 Reply With Quote
Old Apr 11, 2006, 09:44 AM   #2
jeremy.king
macrumors 603
 
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   0 Reply With Quote
Old Apr 11, 2006, 10:04 AM   #3
Eraserhead
Thread Starter
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: 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   0 Reply With Quote
Old Apr 11, 2006, 10:29 AM   #4
jeremy.king
macrumors 603
 
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   0 Reply With Quote
Old Apr 11, 2006, 10:32 AM   #5
jeremy.king
macrumors 603
 
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   0 Reply With Quote
Old Apr 11, 2006, 10:51 AM   #6
Eraserhead
Thread Starter
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: 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   0 Reply With Quote
Old Apr 11, 2006, 11:16 AM   #7
jeremy.king
macrumors 603
 
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   0 Reply With Quote
Old Apr 11, 2006, 01:11 PM   #8
Eraserhead
Thread Starter
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
OK thanks, that appears to work fine!
Eraserhead is offline   0 Reply With Quote
Old May 14, 2006, 10:43 PM   #9
ajoso
macrumors newbie
 
Join Date: May 2006
Dear All,

I've experienced the same problem. Is there any other way to solve it?

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

Thank you
ajoso is offline   0 Reply With Quote
Old May 15, 2006, 04:31 AM   #10
Eraserhead
Thread Starter
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
You're probably having a problem as the script is set up only for me as it creates a folder on my disk which is called "Matthew's HD" try changing that to something else and that should work.

To be perfectly honest though running applescript commands from java in this way is very slow. It is much quicker, more reliable, and importantly doesn't require a password to save the script as an applescript file somewhere memorable (eg. the desktop, but for real use you'd want to save it somewhere else.)
Code:
set thePath to path to desktop
tell application "Finder"
	make new folder in folder thePath with properties {name:"testing"}
end tell
then invoke the command line from the java file and use the 'osascript' command from there, this is shown below:
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Test2 {
		public static String UnixComm(String cmd){
		try{
			Process Results=Runtime.getRuntime().exec(new String[] {
				"/bin/sh","-c",cmd});
			//Results.waitFor(); //- Add this line if and only if you aren't collecting any data but need the command line stuff to have finished before continuing.
			//If you don't need either then you can remove 'Process Results=' from the line above.
			BufferedReader br=new  BufferedReader(new InputStreamReader(Results.getInputStream()));
			String s=br.readLine();
			br.close();
			br=null;
			Results=null;
			return s;
		}
		catch(Exception e){
			return "Error with UNIX Command";
		}
	}
	public static void main (String args[]) {
		String cmd="osascript ~/Desktop/Test.scpt";
		UnixComm(cmd);
	}
}
This returns any values from the unix command as a string so you can manipulate them if you wish.

For more advanced use (eg you want to pass parameters to the script), feel free to ask about that.
__________________
If they have to tell you every day they are fair you can bet they arent, if they tell you they are balanced then you should know they are not - Don't Hurt me

Last edited by Eraserhead; May 15, 2006 at 04:37 AM.
Eraserhead is offline   0 Reply With Quote
Old May 15, 2006, 09:45 AM   #11
jeremy.king
macrumors 603
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
Quote:
Originally Posted by ajoso
Dear All,

I've experienced the same problem. Is there any other way to solve it?

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

Thank you
read post #7 above...
jeremy.king is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Run DreamWeaver CC without Java 5t3f4n Web Design and Development 2 Oct 9, 2013 07:36 AM
Can Not Get Java To Run in a Webview xShane Mac Programming 12 Aug 12, 2013 09:57 PM
Can't run Java jojoba Mac Applications and Mac App Store 9 Apr 23, 2013 10:18 PM
applescript prompts to run Zeke D Mac Programming 4 Jul 23, 2012 09:50 PM
What browser I need to be able to run java ? interinfo iPad Apps 8 Jun 1, 2012 08:11 AM

Forum Jump

All times are GMT -5. The time now is 07:55 AM.

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

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC