Fail to run the demo of this java application - java

I am completely new to Java, just learned a little because I need to run this project: https://github.com/ansjsun/ansj_seg
I have run
mvn compile
and some other stuff.
Now, in ansj_seg/target I have a file named ansj_seg-0.8.jar, which seems important, although I don't know how to use it.
In ansj_seg/src/test/java/org/ansj/demo, there are some demos, and I want to run BaseAnalysisDemo.java, I tried as following.
Step one, I compile using:
javac -classpath ~/Downloads/ansj_seg/target/ansj_seg-0.8.jar BaseAnalysisDemo.java
It works fine and generates BaseAnalysisDemo.class.
But when I try to run it using
java BaseAnalysisDemo
An error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: BaseAnalysisDemo (wrong name: org/ansj/demo/BaseAnalysisDemo)
I guess I should set some path, but have no idea.
Anyone can help?
BTW, I prefer using command line to Eclipse.
The directory structure is like this(Updated):
\ansj_seg
pom.xml
\src
\main
\java
\org
\ansj
\app
\dic
\domain
...
\resource
...
\test
\java
\org
\ansj
\demo
BaseAnalysisDemo.java
Demo.java
...
\test
...
Here is another question, even if the demo can be run, how can I use this java library in another place? I guess the file ansj_set-0.8.jar should be used. Again, I know nothing about Java.. Any suggestion will be very helpful.
Updated:
If I run with classpath specified:
java -classpath ~/Downloads/ansj_seg/target/ansj_seg-0.8.jar BaseAnalysisDemo
still got an error, but different:
Exception in thread "main" java.lang.NoClassDefFoundError: BaseAnalysisDemo

You will fall back to Eclipse soon, because what you'd like to do is a bit cumbersome, but doable.
Try this:
mvn exec:java -Dexec.mainClass="java.org.ansj.demo.BaseAnalysisDemo.java"
If you need arguments too, add -Dexec.args to the list.
Note that you'll have to move your demo under the main source tree.

Related

Run class main from groovy script using grape

I would like to run the main method of a java class by using #Grab so that requirements are taken care of automatically. More specifically I would like to run the pdfbox example https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java
I wrote the following groovy script
#!/usr/bin/env groovy
#Grab('org.apache.pdfbox:pdfbox-examples:2.0.20')
import org.apache.pdfbox.examples.util.RemoveAllText
RemoveAllText.main(args)
The #Grab, import and execution of main seems to work. But the main seems to recall itself repeatedly thus failing with a StackOverflowError as below.
Caught: java.lang.StackOverflowError
java.lang.StackOverflowError
at RemoveAllText.main(RemoveAllText.groovy)
at RemoveAllText$main.call(Unknown Source)
at RemoveAllText.run(RemoveAllText.groovy:5)
at RemoveAllText.main(RemoveAllText.groovy)
...
I am new to groovy so I am not sure what I am doing wrong. Is what I am trying to do possible? If it is possible, how would it be done?
To make the example fully reproducible I get the above error when I use the pdf found at https://github.com/mozilla/pdf.js/raw/v2.4.456/examples/learning/helloworld.pdf and using groovy version 2.4.16 installed using the default repositories in Ubuntu 18.04. The command run would be
groovy RemoveAllText.groovy helloworld.pdf helloworld_out.pdf
If I manually download the required jar files and I run
java -cp pdfbox-2.0.20.jar:commons-logging-1.2.jar:pdfbox-examples-2.0.20.jar org.apache.pdfbox.examples.util.RemoveAllText helloworld.pdf helloworld_out.pdf
it works without problem.
Rename your script from RemoveAllText.groovy to something else and everything should be fine.
Problem that your groovy script produces the same class name as Apache class.

Running my program leads to java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Lscala/collection/immutable/StringOps;

I am trying to run an open-source program called LinkedIn Norbert (https://github.com/linkedin/norbert) in Scala 2.8.1. I have added all the jar files so the program compiles, but when I try to run class com.linkedin.norbert.javacompat.network.RunNorbertSetup in examples/src/main/java, I get the following error:
Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Lscala/collection/immutable/StringOps;
at com.linkedin.norbert.jmx.JMX$.name(JMX.scala:59)
at com.linkedin.norbert.cluster.ClusterClient$$anon$1.<init>(ClusterClient.scala:56)
at com.linkedin.norbert.cluster.ClusterClient$class.$init$(ClusterClient.scala:55)
at com.linkedin.norbert.cluster.zookeeper.ZooKeeperClusterClient.<init>(ZooKeeperClusterClient.scala:22)
at com.linkedin.norbert.cluster.ClusterClient$.apply(ClusterClient.scala:33)
at com.linkedin.norbert.javacompat.cluster.ZooKeeperClusterClient.<init>(ZooKeeperClusterClient.scala:23)
at com.linkedin.norbert.javacompat.cluster.ZooKeeperClusterClient.<init>(ZooKeeperClusterClient.scala:21)
at com.linkedin.norbert.javacompat.network.RunNorbertSetup.configCluster(RunNorbertSetup.java:115)
at com.linkedin.norbert.javacompat.network.RunNorbertSetup.main(RunNorbertSetup.java:21)
What would I need to do to get past this error? I've looked at other Stack Overflow and forum threads, and these have not helped.
FYI: I built the program with SBT.
Thanks so much,
Rebecca
Run a "clean" instead of build. Remove all the build and bin folders from the project directory too.
The solution was to put the relevant class in the classpath.

Unable to run Selenium tests (java) from the command line

I am using Selenium to do tests on some html. I was using java under Eclipse and run test by Junit. And everything was fine. But I got problems when I started use command line (javac/java at Dos prompt) to compile/run the test.
For this java project, I have a bunch of classes, each one represents one test while each each class sits in its own java file. And All these files are under one package/folder called "testSuite". Each test file will import a bunch of jar files. And they also import Global class from another
The problems, the compile was ok. but I make it run. I compiled global.java first then compiled PolicyFilter.java and then try to run the class, below is what I got:
Exception in thread "main" java.lang.NoClassDefFoundError: \testsuite\PolicyFilter (wrong name: testSuite/PolicyFilter)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
Please use java -cp requiredClassfiles/folders/jars classToExecute.
I hope you have Selenium RC Jar available and started it using command java -jar selenium-server-standalone-<version-number>.jar.
Make sure that you put the annotation #Test on the test, clean the folder target in project folder and then restart the eclipse.
Hope it works :))

Odd NoClassDefFoundError

After installing the latest version of Vuze (Azureus), I got an odd error trying to start it:
> java -Xmx128m -classpath ./Azureus2.jar:./swt.jar -Djava.library.path=/bt_work/vuze -Dazureus.install.path=/bt_work/vuze -Dazureus.script=./azureus -Dazureus.script.version=2 org.gudy.azureus2.ui.swt.Main
Exception in thread "main" java.lang.NoClassDefFoundError: org/gudy/azureus2/ui/swt/Main
Caused by: java.lang.ClassNotFoundException: org.gudy.azureus2.ui.swt.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
What's odd is this:
> javap -classpath ./Azureus2.jar:./swt.jar org.gudy.azureus2.ui.swt.Main
Compiled from "Main.java"
public class org.gudy.azureus2.ui.swt.Main extends java.lang.Object{
public static final java.lang.String PR_MULTI_INSTANCE;
...
So ... running javap with the same classpath finds the class but java alone can't. WTF is going on?
I checked that both programs come from the same install Java (/usr/lib64/jvm/java-1.6.0-sun), that's Java 6 and the classes were compiled for Java 5. The manifest isn't signed. The JAR file is readable (unzip -t reports no errors).
NoClassDefFoundError happens when the class itself is found but the class loader cannot load all the classes it needs.
Can you check the import headers for class org.gudy.azureus2.ui.swt.Main.java and make sure that all the imported classes can be found in your classpath. If not, add the jar files to your classpath.
Post the import section if you want me to help figure out what is still needed.
One word: AppArmor
In my case, the config didn't allow the program java to load the JARs from the new installation path.
If you have a similar problem, look into /var/log/audit.log. You should see the error messages there.
Your exception is java.lang.NoClassDefFoundError and not exactly ClassNotFoundException - so javap will still be able to disassemble the class.
As you might know NoClassDefFoundError can be seen as a linkage error. I tend to guess that the runtime is missing some required class to execute org.gudy.azureus2.ui.swt.Main
I guess it requires more JARs on classpath.
So org.gudy.azureus2.ui.swt.Main is available (that is why javap works) but one of its dependency is not found during runtime.
Also running SWT sometimes requires -Djava.library.path set to swt library (looking at your SO reputation I guess you know this)
Edit
Here is a link to one Azureus shell script, which lists more classpath JARs.

Classpath compiles correctly, but will not run. What am I missing

Greetings,
I'm playing around with mahout, I've written a basic java class which imports some of the libraries. It seems my classpath is correct when compiling, I get no errors or complaints at all.
However when I run the compiled class I get an exception saying...
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
My guess is that . is not on your classpath. For example, you might be compiling with:
javac -cp foo.jar:bar.jar Test.java
but then to run the code you'd need
java -cp foo.jar:bar.jar:. Test
The code that you're compiling doesn't need to be on the classpath as you're providing the code (so there's nothing to find) - that's why it manages to compile but not run.
That's only a guess, of course - if you could post the commands you're using to compile and run the code, that would help.
I'm now getting an error saying java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
You're missing slf4j-api.jar on your class path. With SLF4J, you always need slf4j-api.jar and another jar to bind a logging framework. And actually, if you don't care about logging, use slf4j-nop.jar instead of slf4j-log12.jar.
Update: Mahout seems to be available in Maven central repository so using Maven could ease the class path setup process. And if you're not into learning Maven, consider using MOP which is a command line launcher to run Java stuff that can transparently download Maven artifacts and their dependencies and setup your classpath.
Compile time classpath sounds right; runtime classpath is wrong.
From the javadocs for that class:
Thrown if the Java Virtual Machine or
a ClassLoader instance tries to load
in the definition of a class (as part
of a normal method call or as part of
creating a new instance using the new
expression) and no definition of the
class could be found.
The searched-for class definition
existed when the currently executing
class was compiled, but the definition
can no longer be found.
Do you see a Test.class file in the current directory? Maybe you compiled it to another path by mistake.
If you are using Mahout, be aware that after you build it with Maven, it will generate "*.job" files in the target/ directory, which contain all dependencies packaged together. It is just a .jar file.

Categories

Resources