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.
Related
This question already has answers here:
Why can't I run my java Hello World program if it is inside a package?
(3 answers)
Closed 9 years ago.
I'm currently learning Java and the use of my command prompt as a compiler. But every time I execute the java command followed by my test class "Hello" I get the below error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: hello/Hello)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Now I've checked my "CLASSPATH" environment variable and it is correct as follows: .;C:\Program Files\Java\jdk1.7.0_25\bin; I've even tried removing the .; from the beginning of the CLASSPATH but it didn't do anything different. Now my javac command works just fine by creating a .class version of my .java class. But I just can't get it to actually execute the java command.
The name of my class is Hello so I typed javac Hello.java to compile my file as a class file and it worked. But when I enter: java Hello is when I get the above error messages. I've tested the program on my NetBeans IDE that I made it in, and it works perfectly fine with no errors.
What could possibly be going on that would prevent me from executing my java command to run a .class file?
The name of my class is Hello so I typed javac Hello.java to compile
my file as a class file and it worked. But when I enter: java Hello
The most likely problem is that while running your java program you are not putting the complete class name along with the package structure. It should be run as mentioned here:
java packagenhierarchy.Hello
Assuming your package name is com.my.hello and your main class name is Hello then it should be run from the directory containing the top level package as:
java com.my.hello.Hello
UPDATE: As per your comments and knowing the working directory, here is what you should run:
java -cp C:\hello\src\hello hello.Hello
You need to understand how java tool works which is little different than how javac works. In order to run a program with java command-line command:
The class that has the main(String[] args) method should be in the classpath.
Instead of type java Hello you should use the fully qualified name, such as:
java com.mypackage.Hello
assuming that you set the classpath with the variable CLASSPATH. Otherwise, it should be like this:
java -cp C:\projects\myprojct\bin com.mypackage.Hello
assuming that bin is the root directory that has the following hierarchy:
bin -
|
com -
|
mypackage -
|
Hello.class
Note that if you don't use neither CLASSPATH nor -cp nor -classpath, then the current directory is by default is in the classpath. In other words, the following should work:
cd C:\projects\myprojct\bin
java com.mypackage.Hello
Maybe your current directory is not in the Class path. Try
java -cp pathToYourHelloCompiledFile Hello
You must have a Hello.class file in your above folder whose path you provide as the class path.
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 \\*.
I built a java program that runs the command "jps" and sees all JVMs and kills a particular JVM by extracting its id from the output of JPS command. It is working fine when I run it on the ubuntu terminal. But then I wrote a script in bash to ssh that machine from other machine and run this program there.
ssh $host "java -cp daemon.jar JVMname;"
Now here comes the problem.
Exception in thread "main" java.io.IOException: Cannot run program "jps": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) at runtime.daemon.halt.main(halt.java:19)
Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
If I go to the the machine myself and run this same command it works. I do not want to switch to exec solution.
Any ideas
Thanks
This isn't a Java issue per se, it's down to the difference in your environment in both cases. Specifically, when you run interactively, your $PATH variable contains the directory for jps, whereas in the latter case via SSH it doesn't.
Have a look at the .bash_profile and .bashrc files on the remote machine - I suspect the path will be set in the .bash_profile file, which isn't executed for non-login shells (such as your SSH invocation that runs a single command). If you set the path correctly in .bashrc, then your current invocation should start working.
(Note this assumes you're using bash for a shell, though most other shells have a similar distinction between the login shell and non-login shell init files.)
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