Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
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

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
kingjr3 said:
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...
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
Eraserhead said:
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

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
Eraserhead said:
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.
 

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
kingjr3 said:
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.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
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
 

ajoso

macrumors newbie
May 14, 2006
1
0
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
 

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
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. :)
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
ajoso said:
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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.