HtmlUnit first steps - java

I am trying to set up my first example program with HtmlUnit. This is the code:
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.WebClient;
public class test {
public static void main(String[] args) throws Exception {
WebClient client = new WebClient();
HtmlPage currentPage = client.getPage("http://www.oddsportal.com/matches/soccer");
client.waitForBackgroundJavaScript(10000);
String textSource = currentPage.asXml();
System.out.println(textSource);
}
}
then i compile:
javac -cp lib/htmlunit-2.9.jar test.java
but when i try to exec test i get
java -cp lib/htmlunit-2.9.jar test
Exception in thread "main" java.lang.NoClassDefFoundError: test
Caused by: java.lang.ClassNotFoundException: test
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: test. Program will exit.
where is the problem? i lack some other packages?

you may have to add the current path to the classpath.
On Unix/Linux/Mac:
java -cp .:lib/htmlunit-2.9.jar test
On Windows:
java -cp .;lib/htmlunit-2.9.jar test
EDIT
There is actually more jars needed for htmlunit that just htmlunit-2.9.jar. Therefore, considering that all those required jars are located in the lib/ directory, you should actually invoke the following:
On Unix/Linux/Mac:
java -cp .:lib/* test
If you run this command from a shell, you also need to escape the wildcard (with a '\') or put the whole classpath within quotes to avoid shell expansion.
On Windows:
java -cp .;lib/* test

This works:
java -cp .:lib/* test

Related

Jython How to Use Standalone JAR?

I am trying to use the Jython PythonInterpreter class, but am struggling a bit.
Due to physical limitations, my scenario lacks any sort of project structure. I simply have a Jython JAR file and a Java file.
I am currently compiling my Java code like so:
javac -cp "jython-standalone-2.7.0.jar" test.java
This command does not fail.
When I try to run the compiled Java, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/python/util/PythonInterpreter
at test.main(test.java:9)
Caused by: java.lang.ClassNotFoundException: org.python.util.PythonInterpreter
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
The code looks like so:
import org.python.util.PythonInterpreter;
class test {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
}
}
The code is very simple. Should I be importing the Jython classes a different way? Does my method here have any chance of working?
Thanks
How do you run the command?
You will also need to set the CLASSPATH.

Works in sbt but not raw Java: after using `javac` to compile a .class file, why can't `java` find it?

I used ObjectWeb's ASMifier to get a 'HelloDump.java', and added classloader and a main method to load and run a spoofed "HelloWorld".
If I run 'HelloDump.java' in the build tool sbt, everything works fine and outputs "HelloWorld!".
But if I use raw Java, it breaks. 'HelloDump.java' seems to compile OK, but the resulting 'HelloDump.class' is clearly present, yet doesn't seem to be recognized (check the sequence below):
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ javac -cp lib/asm-all-4.1.jar HelloDump.java
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ ls
DumpLoader.java.bak HelloDump.class.bak Hello.java.bak
Hello.class.bak HelloDump$DynamicClassLoader.class lib
HelloDump.class HelloDump.java README.md
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ java -cp lib/asm/all/4.1.jar HelloDump
Exception in thread "main" java.lang.NoClassDefFoundError: HelloDump
Caused by: java.lang.ClassNotFoundException: HelloDump
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: HelloDump. Program will exit.
Explicitly adding . to the classpath doesn't help either.
So why does this work in sbt, but not in raw Java, and how can I fix it?
Thanks, any advice is appreciated,
-Julian
Add . to your CLASSPATH, so your command becomes:
java -cp lib/asm-all-4.1.jar:. HelloDump
The reason the JVM cannot find the class is that it looks only on the classpath and nowhere else.

How to compile and run Stanford's Karel with the command line?

This program compiles and runs swimmingly in eclipse on the same machine, but I'd really like to work from the command line and my editor of choice.
CollectNewspaperKarel.java
import stanford.karel.*;
public class CollectNewspaperKarel extends SuperKarel {
public void run() {
// You fill in this part
}
}
The karel.jar is in the same directory as the file above.
Compile
javac -cp karel.jar CollectNewspaperKarel.java
with no errors.
Run
java -cp karel.jar CollectNewspaperKarel
Exception in thread "main" java.lang.NoClassDefFoundError: CollectNewspaperKarel
Caused by: java.lang.ClassNotFoundException: CollectNewspaperKarel
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)
you forgot to include your class to classpath when running the program.
Try this - if running on windows:
java -cp karel.jar;CollectNewspaperKarel.class CollectNewspaperKarel
or this if you're running on linux:
java -cp karel.jar:CollectNewspaperKarel.class CollectNewspaperKarel
One more thing however, you need to have main method in your class, otherwise it won't work :)
Update:
I've found following site: http://ycsoftware.net/setting-up-karel-the-robot-in-eclipse/
seems, you should go with the following arguments if you have the same version of karel as the author there:
on windows:
java -cp karel.jar;CollectNewspaperKarel.class stanford.karel.Karel code=CollectNewspaperKarel
on linux:
java -cp karel.jar:CollectNewspaperKarel.class stanford.karel.Karel code=CollectNewspaperKarel
Peter B. is right, however you cannot run a class containing a "run" method alone, you need a public static void(String[] args) method to make it runnable.
I suppose that in Eclipse some other class is used as a "main class" to run the thing.

java.lang.ClassNotFoundException while executing a class file from inside a jar

I created a jar file in windows & copied it to linux and tried to execute with the following command:
java -jar Test.jar com.dcat2.messaging.datatransfer.Test
I am getting an exception as follows:
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Test. Program will exit.
I am executing this command in the same location where i have the jar file.
Here is my java code:
package com.dcat2.messaging.datatransfer;
public class Test {
public static void main(String[] args) {
System.out.println("Test App : " + args[0]);
}
}
The jar -tf command's output is as follows:
jar -tf Test.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/dcat2/
com/dcat2/messaging/
com/dcat2/messaging/datatransfer/
com/dcat2/messaging/datatransfer/Test.class
Can anyone help please?
java -cp Test.jar com.dcat2.messaging.datatransfer.Test
The -jar option launches the class that is indicated as the main class in the manifest of the jar file (and which must be the Test class, in the default package, in your case).
The -cp option sets the classpath to use for the app which, in this case, only contains your test jar.

NoClassDefFoundError thrown when specifying same jar file at compile and run time

Hi I'm trying to use an external Java package from my own code and keep getting NoClassDefFoundError even though I am using the same class path that I compiled with.
For your amusement I have included a bare bones reproduction of what will undoubtedly be a dumb mistake on my part (I've been at this for about 6 hours so far)
/* WTF/WTF.java */
import foo.Bar;
class WTF
{
public static void main(String[] args)
{
Bar dontCare = new Bar();
}
}
/* WTF/foo/Bar.java */
package foo;
class Bar
{
public Bar() {}
}
Now from the WTF directory I run the following:
javac foo/Bar.java [ok]
javac WTF.java [ok]
java WTF [ok]
jar cf foo.jar foo [ok]
I remove the WTF/foo directory so there is only WTF/foo.jar available.
javac WTF.java -cp foo.jar [ok]
java WTF -cp foo.jar [$!##$!]
Exception in thread "main" java.lang.NoClassDefFoundError: foo/Bar
at WTF.main(WTF.java:7)
Caused by: java.lang.ClassNotFoundException: foo.Bar
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Any clues greatly appreciated!
The directory where is WTF.class should be in the classpath.
Also, Bar should be public.
java -cp foo.jar:. WTF
: is the path separator in Linux, if you are using Windows replace it with ;.
Wrong arguments sequence? Try:
java -cp foo.jar WTF

Categories