Question

I'm trying to call a Java class method from the command line but the for the life of me can't figure out the syntax. The method is stringToString found in this class http://apidoc.ch.cam.ac.uk/oscar3/apidocs/uk/ac/cam/ch/wwmm/oscar3/Oscar3.html

What I have tried:

java -Xmx512m -classpath .:oscar3-a5:lib/oscar3-a5.jar uk.ac.cam.ch.wwmm.oscar3.Oscar3 stringToString "test"

java -Xmx512m -classpath .:oscar3-a5:lib/oscar3-a5.jar uk.ac.cam.ch.wwmm.oscar3 Oscar3(stringToString "test)

java -classpath .:oscar3-a5.jar:lib/uk.ac.cam.ch.wwmm.oscar3.Oscar3 stringToString "test"

It complains like this:

Exception in thread "main" java.lang.NoClassDefFoundError: uk/ac/cam/ch/wwmm/oscar3/Oscar3
Caused by: java.lang.ClassNotFoundException: uk.ac.cam.ch.wwmm.oscar3.Oscar3
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)

I have also looked at this (Java: how to import a jar file from command line) for a solution but clearly I'm too retarded to see the obvious. If anyone knows the correct syntax for this any help would be great.

Was it helpful?

Solution

You are very close! I think you want the following:

java -Xmx512m -cp oscar3-a5/oscar3-a5.jar uk.ac.cam.ch.wwmm.oscar3.Oscar3 stringToString test

Alternatively if your current working directory (pwd) is the same directory that oscar3-a5.jar resides in, then:

java -Xmx512m -cp ./oscar3-a5.jar uk.ac.cam.ch.wwmm.oscar3.Oscar3 stringToString test

(I've assumed you're running on *nix - if you're running on Windows replace the / with \)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top