I am very new to Apple scripting so please bear with me. I need to run a .jar file using applescript, the jar is not executable so I invoke the class like com.path.to.myClass. My Apple script looks like below-
display alert "You are about to start the image rename process." buttons {"OK", "Cancel"}
set theAnswer to button returned of the result
if theAnswer is "OK" then
do shell script "java -classpath ./ImageRename_JAVA-1.0.0.jar:. com.mff.image.rename.Main"
else
say "Exit"
end if
Both the applescript and the ImageRename_JAVA-1.0.0.jar are in the same directory, but when I run the script it gives me an error-
error "Exception in thread \"main\" java.lang.NoClassDefFoundError: com/mff/image/rename/Main
Caused by: java.lang.ClassNotFoundException: com.mff.image.rename.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)" number 1
Am I setting the classpath wrong? If so, what is the correct way? Also, how can I add more jars to the classpath?
When I run the below command from Terminal it runs just fine.
$ java -classpath ./ImageRename_JAVA-1.0.0.jar:. com.mff.image.rename.Main
I know that it can be done in a better way using JAR Bundler but I have no control over the JAR and its developed by someone else. Is there a way that I can include all the JARs inside the application under YourApplicationName.app/Contents/MacOS/Resources/Java/ directory and use those in the class path.
I don't think you can guarantee what the working directory is in a do shell script, but you can work it out with something like this:
set scriptPath to the POSIX path of (path to me)
do shell script "SCRIPTDIR=`dirname " & scriptPath & "` ; " ¬
& "java -classpath $SCRIPTDIR/ImageRename_JAVA-1.0.0.jar:$SCRIPTDIR com.mff.image.rename.Main"
To add extra JARs to the classpath you can take advantage of a shortcut provided by the java command whereby a classpath entry ending in * includes all .jar files in the given directory.
do shell script "java -classpath " ¬
& "/Applications/Something.app/Contents/Resources/Java/\\* com.example.MyClass"
The * needs to be backslash escaped to protect it from expansion by the shell, and the backslash itself needs to be backslash-escaped when it is within an AppleScript string literal, hence the \\*.
Related
i have a windows batch script which launches a jar which launches a game written all in Java (its a runescape client). It should work.
The original Batch (WIN):
#echo off
#echo Client Is loading......
#echo -----------------------
java -Xmx1000m -cp .;Theme.jar Gui 0 0 highmem members 32
pause
the Shell file ive made for OS:
#!/bin/sh
echo Your client is loading...
echo --------------------
java -Xmx1000m -cp Theme.jar Gui 0 0 highmem members 32
the error in terminal:
Your Client is loading...
--------------------
Exception in thread "main" java.lang.NoClassDefFoundError: Gui
Caused by: java.lang.ClassNotFoundException: Gui
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
How can i fix it or make a shell script that will do the exact as the Batch and actually run?
You didn't convert the shell script correctly. In the Windows version the -cp parameter is .;Theme.jar, so in Linux it should be .:Theme.jar, the path separator ; replaced with :, like this:
java -Xmx1000m -cp .:Theme.jar Gui 0 0 highmem members 32
A ClassNotFoundException in general signals something wrong with the classpath. (The -cp parameter is a shortcut for -classpath).
Judging by the Windows script, Gui is a class name, and the rest are command line arguments passed to the Gui class. The error message tells you it cannot find the Gui class. It must be either in the current directory or the theme.jar. If it's not in any of them then this can't work.
java.lang.ClassNotFoundException
I think there might be two reason:
The jar file which you are including while running the java program is not included into the jar file which you have created for shell script.
You have not defined Main-Class in manifest.mf file while you created your jar file.
Try using this,
java -Xmx1000m -cp .: Theme.jar Gui 0 0 highmem members 32
as ':' is the classpath seperator for Unix environments while ';' is for Windows.
The ClassNotFoundException questions are found by the dozens on SO but even so I did not find the answer to my problem in past answers :(
Basically I get a ClassNotFoundException while trying to run a large open source program from the command line using a command prompt that is provided in the project's doc. The prompt is as follow:
java -cp "target/classes/*;../../Algotrader/code/target/classes/*;../../lib/*;../../target/*" -Dsimulation=true -DdataSource.dataSet=1year com.algoTrader.starter.SimulationStarter simulateWithCurrentParams
(note: the original command actually says java.exe but I changed it to java as java.exe is not recognised as a command on my Mac)
The exception is thrown for the SimulationStarter class as shown by the stack trace:
Exception in thread "main" java.lang.NoClassDefFoundError: com/algoTrader/starter/SimulationStarter
Caused by: java.lang.ClassNotFoundException: com.algoTrader.starter.SimulationStarter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
From Eclipse I can see that SimulationStarter.class is in AlgoTrader/code/target/classes/com/algoTrader/starter, which looks in line with the path provided in the command prompt.
So my question is: what could be the cause(s) for this exception other than the class being incorrectly placed in the classpath?
Also not sure this makes any difference but the project is kept under svn and Maven and I am running it on a Mac.
CORRECT CLASSPATH
In the end the classpath given in the command prompt was at fault. The correct paths (at least ones that solve the problem) are:
java -cp "target/classes/:../../Algotrader/code/target/classes/:target/*" -Dsimulation=true -DdataSource.dataSet=1year com.algoTrader.starter.SimulationStarter simulateWithCurrentParams
The main differences with the original prompt is the removal of the stars except for the folder that contains the jar files and the shortening of the base classpath paths to capture only the base directories. Also ":" should be used instead of ";" on Macs and Linux as per
#reprogrammer answer
Are you sure you need all the * there?
Usually, you will want to give the base directories of the classpath, not subfolders.
E.g. when building your application into "bin", you would use java -cp bin mainclass, not java -cp bin/*! The support for * is generally a bit flaky, as it is a shell metacharacter, and you need to get quoting right. It can really screw you if you have an incorrect classpath. I've seen people have issues because they added README.TXT to their classpath.
The classpath syntax is OS-dependent. The classpath separator in Linux and Mac OS X is : not ;.
This is for a class. I've never used shell scripting before, but the instructions are to write a script that runs my java program (which is already all set up to take in command line arguments (at least one arg, with an optional second one)). I'm supposed to be able to run it like this:
./script.sh arg1 arg2
But when I do, I get the following error (my java main class name is A1):
Exception in thread "main" java.lang.NoClassDefFoundError: A1/class
Caused by: java.lang.ClassNotFoundException: A1.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: A1.class. Program will exit.
This is what my shell script looks like right now:
#!/bin/sh
java -Xmx1024m A1.class $#
Any help is appreciated.
You need to tell java where to look for your class (using -cp) - either in a directory or a .jar
To find the directory where the script is (as opposed to where it was launched from), you can use: $(dirname $0).
So, for example:
#!/bin/bash
JVM=java
JVM_OPTS="-Xmx1024m"
$JVM $JVM_OPTS -cp $(dirname $0)/myapp.jar A1 "$#"
It's a good idea to be explicit about the shell you want. Also note the quotes around $#, needed for escaped args.
Use the classpath flag when running java to tell the virtual machine where your A1.class file is located.
See the doc
Should be something like:
java -classpath /myfolder A1 $#
Also, do not use the ".class" suffix when running the command.
I have downloaded and unzipped the following WEKA version weka-3-4-19. This is on a linux operating system. I wish to use WEKA through the command line, however on executing
java weka.classifiers.tress.j48.J48
I get the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: weka/classifiers/tress/j48/J48
Caused by: java.lang.ClassNotFoundException: weka.classifiers.tress.j48.J48
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: weka.classifiers.tress.j48.J48. Program will exit.
Can someone help me resolve this? Thank you.
Edit1:
On trying the java -jar weka.jar command
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Window.<init>(Window.java:432)
at java.awt.Frame.<init>(Frame.java:403)
at javax.swing.JFrame.<init>(JFrame.java:202)
at weka.gui.GUIChooser.<init>(GUIChooser.java:98)
at weka.gui.GUIChooser.main(GUIChooser.java:285)
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
Edit 2:
On trying java.weka.classifiers.trees.J48
Exception in thread "main" java.lang.NoClassDefFoundError: weka/classifiers/tress/J48
Caused by: java.lang.ClassNotFoundException: weka.classifiers.tress.J48
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: weka.classifiers.tress.J48. Program will exit.
Edit 3:
{cslinux2:~/weka-3-4-19} echo $CLASSPATH
/people/cs/j/jld082000/weka-3-4-19/weka.jar:
{cslinux2:~/weka-3-4-19} java weka.classifiers.trees.J48
Weka exception: No training file and no object input file given.
General options:
-t <name of training file>
Sets training file.
-T <name of test file>
Sets test file. If missing, a cross-validation will be performed on the training data.
That simply means weka.classifiers.tress.j48.J48 class is not in classpath. You can write java command with -classpath switch or set CLASSPATH (permanent) variable. Another way is to use -jar switch as pointed by #jberg.
EDIT:
As I checked (I downloaded Weka 3-4-19 from that site) there is definitely no weka.classifiers.tress.j48.J48 class in weka.jar package. Probably you are looking for:
java weka.classifiers.trees.J48
For example:
$ export CLASSPATH=/home/grzegorz/weka-3-4-19/weka.jar:.
$ echo $CLASSPATH
/home/grzegorz/weka-3-4-19/weka.jar:.
$ java weka.classifiers.trees.J48
Weka exception: No training file and no object input file given.
General options:
-t <name of training file>
Sets training file.
...
This "weka.classifiers.trees.j48.J48" is a typo in the Weka documentation. It's should be this: "weka.classifiers.trees.J48"
And instead of setting the $CLASSPATH the alternative is just to put:
java -cp /pathto/weka.jar weka.classifiers.trees.J48
Also, you might want to give it more memory to play with, to speed things up:
java -Xmx1G -cp /pathto/weka.jar weka.classifiers.trees.J48
For running a classifier (like you are attempting to do) you need to at least give it some data, which must already be converted to ARFF format.
To run a test on some data enter:
java -Xmx1G -cp /path/to/weka.jar weka.classifiers.trees.J48 -t /path/to/whatever.arff
-t is for training file.
See here:
Weka Primer
I haven't used WEKA on linux, but I think it is just packaged as a jar file, so you want to:
java -jar weka.jar
You can also use the weka source jars to use the classifiers in your own code by including it in your build path like you would other jars.
I run the Linux Developer version in macOS. You can copy the .bash_profile below and modify to your needs.
As I answered here, you can just put the following to your ~/.bash_profile
export R_HOME="/Applications/R.app/Contents/MacOS/R" #for WEKA MLR R plugin
export CLASSPATH="/Applications/weka-3-9-1/weka.jar" #for WEKA commandline
export WEKAINSTALL="/Applications/weka-3-9-1"
export WEKA_HOME="/Applications/weka-3-9-1"
export CLASSPATH=$CLASSPATH;$WEKA_HOME/weka.jar
export HEAP_OPTION=-Xms4096m -Xmx8192m
export JAVA_COMMAND java $HEAP_OPTION
after this and refreshing the terminal, you should be able to run the following command
java weka.classifiers.trees.J48 -t $WEKAINSTALL/data/iris.arff
I have such bash script start.sh:
export JAVA_HOME=/home/qds/bin/jdk1.6.0_22
export QDS_HOME=/home/qds
$JAVA_HOME/bin/java -classpath $QDS_HOME/lib/*:$QDS_HOME/lib/commons/* com.qds.Main $#
In the directory /home/qds/lib I have necessary libraries and my jar file, wich contains:
com\qds\config
com\qds\entities
com\qds\hibernate
com\qds\protocols
com\qds\util
com\qds\Main.class
but when I run ./start.sh, I have:
./start.sh
Exception in thread "main" java.lang.NoClassDefFoundError: com/qds/Main (wrong name: Main)
at java.lang.ClassLoader.defineClass1(Native Method)
You have to specify what your main class is going to be in one of several ways
specify the main class name as an argument to the java command (just add the main class to the end of the your java command)
Define it in your main jar's manifest
Java does not support wildcards in option -classpath. Try to remove * from $QDS_HOME/lib/* and from $QDS_HOME/lib/commons/*
BTW this is relevant for windows too.
Classpath should contain
1. directories
2. explicitly written jar files.
If you have many jar files you can create script that composes the command line option.
For example on linux
-classpath `ls -1|tr '\n' :`
composes classpath delimited with colon