I'm trying to specify the classpath of my package when i run my program, like this:
java –cp .;my.package.jar MyMainClass
my.package.jar lies in the same directory as MyMainClass.class.
I'm getting this error message:
Could not find or load main class ?cp
It seems like it is not recognizing the switch -cp, and is instead trying to find a class named cp. Anyone know what may cause this?
If you are on *nix, the class path separator is :, not ;. In that case you need to run java –cp .:my.package.jar MyMainClass.
Otherwise if you are on widows, that ? you get in the error message looks suspicious. Are you some way copy-pasting the command and the - isn't really a regular -? Try typing the command into the terminal manually.
It looks like you are using old version of Java which recognizes only full name - use
-classpath instead of -cp
Related
I want to use an environment variable as a JVM option when executing java -jar.
The command I want to execute it:
java -XX:onOutOfMemory='echo test' -jar foo.jar
When I run the above command as is, the jar will run.
(If you don't have the foo.jar, you will get an Error: Unable to access jarfile foo.jar error. But this still means that the option gets used correctly).
But when I create an environment variable containing the JVM option, and run the command using that variable.
OOM="-XX:onOutOfMemory='echo test'"
java $OOM -jar foo.jar
Than I get the following error:
Error: Could not find or load main class test'
It seems like the java command is ignoring the quotes around 'echo test'.
After looking for similar questions on SO and on other websites, I tried various variations of using quotes:
OOM="-XX:OnOutOfMemoryError=\"echo test\""
OOM='-XX:OnOutOfMemoryError="echo test"'
OOM=-XX:OnOutOfMemoryError="echo test"
But they all result in the same error.
An article from Oracle concerning JVM options, mentions using a semicolon:
-XX:OnOutOfMemoryError="<cmd args>; <cmd args>"
But the purpose of the semicolon is to separate multiple commands, not command and arguments. So this does not fix my problem.
Does anybody know how I can correctly move the -XX:onOutOfMemory='echo test' option into an environment variable?
When running java, you should quote $OOM
Example:
java "$OOM" -jar foo.jar
See Why does my shell script choke on whitespace or other special characters? on Unix stackexchange for why this is needed.
I'm trying to open an xterm terminal in Java, and run a Java file in it. Here's the Java code that is opening up the terminal:
Process p2 = new ProcessBuilder("xterm", "-hold", "-e", "java", "/home/harry/main.class").start();
xterm opens fine, but it's saying that it can't find the main class home.harry.main.class. I'm using slashes, but they're being changed to dots. What am I doing wrong?
The mention of xterm is misleading. The question (agreeing with #elliott-frisch and #user2533521) is how to run a Java class on the command-line. The full pathname and classfile name are two aspects which have to be separated.
Not quite a duplicate, these links can give some insight:
How to run this java class in command line in windows?
How to execute a java .class from the command line
That is (referring to the documentation):
the "/home/harry" can be specified using the classpath -cp option (see Setting the class path).
the ".class" suffix is not useful; only the class name is used (probably "main").
I add the path of jdk in the environment variable "path", my position is on the directory of the application: "C:\Users\20900994t\Desktop\applicationArbre_2_1\src\applicationarbre"
I have 4 .java files .
I tried 2 methodes.
The first method were:
1. "javac Main.java" 2. "java Main"
The second method were:
1. "javac *. java" 2. "java Main.java"
These methods generate all of the files . classes of all the classes I have, but at the command line it shows me that the purpose of the other classes are not recognized and when I did "java Main", it shows me "NoClasseDefFoundError"
Actually, those method works well in environment Net Beans, however I need to execute it on command line with .bat file .
Thank's
I suspect you're running it like this:
// In directory applicationarbre
$ java Main
You should be running it like this, in the parent directory:
$ java applicationarbe.Main
You say you've tried several solutions, but you haven't explained what you've tried, or what happened when you tried them, which makes it hard to help you further.
Basically, you need to tell Java the fully-qualified name of the class you want to launch, and that class has to be available on the classpath, which is "the current directory" by default. Anything more than that and you'll need to give us more information.
Not like this:
java applicationarbre/Main
but like this:
java applicationarbre.Main
As for the classpath, maybe in your case this will be enough (if you are in the correct base directory below which are your classfiles):
java -cp . applicationarbre.Main
first compile the class in which main method resides
C:\foldername>javac ClassName.java
then run with statement
C:\foldername>java ClassName
hope it will work :)
I'm trying to compile a Java project under Cygwin using a native Win32 Java.
The Java binaries are correctly found under /cygdrive/c/jdk/bin on my machine.
The following command works fine:
javac -d . ./gnu/kawa/util/PreProcess.java
The PreProcess.class file is generated in ./gnu/kawa/util/. Trying to invoke Java on this fails however:
CLASSPATH=.:$CLASSPATH java gnu.kawa.util.PreProcess \
%java6 +use:com.sun.net.httpserver +enable:XML \
`sed -e 's|\([^ ]*\)|./\1|' < ./patch-source-list`
Error: Could not find or load main class gnu.kawa.util.PreProcess
...
This command was invoked by make, that's where the $CLASSPATH variable is set dynamically. patch-source-list is just a list of class names. The : in the classpath looks suspicious, but I'm not sure how to test ; while not annoying sh.
My only other suspicion is that the native Java is trying gnu\kawa\util\PreProcess, but I think cygwin can transparently handle that.
Any ideas? Thanks for your time.
Another option would be to build your path using the ':' and then fix the results using cygpath. This is probably overkill in your specific situation, but in a general case where you may have references to multiple directories, some of which may be referenced as absolute rather than relative paths, or if you are working with cygwin symlinks, it is much more useful.
$ ln -s /cygdrive/c/development/libraries/ ../libs
$ cygpath -pw /cygdrive/c/development/:.:../libs
C:\development\;.;C:\development\libraries\
so then you'd build your CLASSPATH variable as before, and in the final stage run
CLASSPATH="`cygpath -pw "$CLASSPATH"`" java (whatever)
Remember, the JVM has no idea that you are using the cygwin bash shell.
Two things:
for the classpath locations, use the windows path names. Thus, no "/cygdrive/c/somepath", but "c:\somepath\" ("/" and "\" can be used interchangeably however)
use ';' instead of ':' in the classpath list
This should work:
export CLASSPATH="./gnu/kawa/util/PreProcess.class"
CLASSPATH=".;$CLASSPATH" java gnu.kawa.util.PreProcess
The : in the classpath looks suspicious, but I'm not sure how to test ; while not annoying sh.
You're exactly right: you need to use ; instead of :. As for how to use it — as Mat alludes to above, you need to "quote" the semicolon. Any of these will work:
CLASSPATH=.\;$CLASSPATH java Foo
CLASSPATH=.';'$CLASSPATH java Foo
CLASSPATH='.;'$CLASSPATH java Foo
CLASSPATH=".;$CLASSPATH" java Foo
You can use whichever one you like best. (The first uses a backslash, which quotes a single following character. The second and third use single-quotes, which quote a sequence of zero or more characters. The fourth uses double-quotes, which are like single-quotes except that they still allow the variable $CLASSPATH to be expanded. For that matter, you could also write something like
CLASSPATH=".;"$CLASSPATH java Foo
if you want. See the above link for lots more information about quoting in Bash.)
I'm playing with Rhino, and I've had success using Java classes from the stdlib, but not from Java code I compiled here.
For example, this works fine:
print(new java.util.Date());
But with NanoHTTPD (single .java file, no namespace, same folder), I'm having no luck at all:
js> new Packages.NanoHTTPD()
js: "<stdin>", line 4: uncaught JavaScript runtime exception: TypeError: [JavaPackage NanoHTTPD] is not a function, it is object.
at <stdin>:4
I'm sure it's something simple. What am I missing?
EDIT: I'm launching it like this:
$ CLASSPATH=. java -jar rhino.jar
or this:
$ java -classpath . -jar rhino.jar
Or I moved NanoHTTPD.java into the folder "./nano", added package nano; to the top of the file, compiled it, and then replaced "." with "nano" in the above classpath assignments.
Any way I do it, from in the interpreter I see:
js> java.lang.System.getProperty("java.class.path")
/Users/me/blah/rhino.jar
You need to run Rhino like this:
java -cp /path/to/rhino/js.jar:. org.mozilla.javascript.tools.shell.Main
This adds the current directory to the classpath. Using -jar clobbers the classpath. (The classpath separator depends on your OS.)
Then try
js> Packages.NanoHTTPD
[JavaClass NanoHTTPD]
If it says [JavaPackage NanoHTTPD], it means it hasn't found a class by that name.
You can't instantiate NanoHTTPD anyways, so I'm guessing you want to try Packages.NanoHTTPD.main([]) or something.
In my Linux, I found that the command 'rhino' is a shell script that runs 'org.mozilla.javascript.shell.Main' with the option '-classpath'. You can edit the file to include the path to your class.
I think the script is self explanatory.
If you use Linux, type:
less `which rhino`
If you don't plan to use your own clases in Rhino usually you run it in following way:java -jar ./js.jar
The problem to use the -jar switch is that you can't define classpath in this case and without setting classpath you can't access to your own packages and classes.To be able to set classpath you need to run Rhino using -cp switch. In this case you set your classpath by -cp switch which shall include package of Rhino and your packages and also you need pass Rhino's main class path inside the package (org.mozilla.javascript.tools.shell.Main)
Here is an example how to add your own packages to Rhino classpath:
Suppose you have your class mypackage.myclass placed in mylib.jar If you want to get this class available in your Rhino session you need to run Rhino in following way:
java -cp "./js.jar;../mylib.jar" org.mozilla.javascript.tools.shell.MainThen you can access to your class:jc> mc_obj = new Packages.mypackage.myclass()
Ensure that the current directory is included in your classpath. The default classpath is the current directory but if the classpath has been set to something else (say by the rhino startup script) then you could run into this.
You might also try placing your test class in a package just to see if it has some quirk with top-level classes.