Javac compilation and execution of 2 java files - java

The program is written with 2 files filter.java - in which is the main function and ClasifiedWord.java which is only container class. I wrote that on windows on eclipse but want to compile it on Linux javac compiler.
javac filter.java ClasifiedWord.java
runs without mistakes, but when I try to run the program:
snowy:Filter$ java filter
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
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)
Could not find the main class: filter. Program will exit.
snowy:Filter$ javac filter.java ClasifiedWord.java
snowy:Filter$ java filter ClasifiedWord
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
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)
Could not find the main class: filter. Program will exit.
How can I run the program ? It seems that the compilation is ok. I have doubts that maybe I have made a mistake about the file paths in the program.... but I think that this is not the case?
Thanks for your answers!

Type the following command.
java -classpath . filter
If you want to type just java filter, please following these guides.
Linux http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/
Windows http://ac-support.europe.umuc.edu/~arnoldyl/NetBeansTutorials/Setting-Classpath.html
P.S. The last command java filter ClasifiedWord means to run a program filter and use a string ClasifiedWord as an argument.

Roland is right in the comment, you should be able to run it with java -classpath <folderContainingDotClassFiles> filter. Also, you should change filter.java to Filter.java as the standard Java convention for class names (including the files containing them) is to capitalize the first letter.

This looks like you either have a wrong CLASSPATH setting (usually you don't need this at all, but if you have it, it should include . in the list, for the current directory), or you are using packages (wrongly), or both. For the classpath issue, see the other answers.
If you are using packages, i.e. your files contain a package ...; line at the beginning, you should put your source files in a directory structure according to the package structure. For example, if you have package example;, you should have an example directory in your current directory, and in this the two source files.
Then call compiler and interpreter like this:
javac example/filter.java example/ClasifiedWord.java
java example.filter
(This assumes that filter is the main class, otherwise replace its name.)

Related

java.lang.NoClassDefFoundError when using weka.jar file in my own java file

I'm having problems executing my java program. The code is placed in the file RunMining.java and contains multiple import weka.[..] lines. The weka library (weka.jar) is placed in the same folder.
I compile it:
$ javac -cp weka.jar RunMining.java
This creates three files: RunMining.class, RunMining$1.class, RunMining$classifierType.class
When i run it: java -cp weka.jar RunMining i get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: RunMining
Caused by: java.lang.ClassNotFoundException: RunMining
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)
My RunMining.java file only contains one class called RunMining, which contains the main method. I would prefer not to add the weka.jar file to my CLASSPATH since this file will be compiled using a makefile and executed using a script, on different machines.
Any suggestions, to how i can run the compiled files?
You need to add the classpath of your compiled files as follows:
java -cp weka.jar;. RunMining
This assumes your files are compiled in the root directory which your javac command suggests.

configuring classpath in java

I am new to java..
I have an example structure as follows:
/folder/foo.java /folder/bar.java /folder/foobar.java
Now I am trying to run foobar.java but I get the following exception
Exception in thread "main" java.lang.ClassNotFoundException: /folder/foobar
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.apache.hadoop.util.RunJar.main(RunJar.java:149)
Then I tried to configure it by doing
java -classpath . foobar
Exception in thread "main" java.lang.NoClassDefFoundError: foobar
Caused by: java.lang.ClassNotFoundException: foobar
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)
Any clues?
Thanks
The default value of a classpath is ‘.’ , i.e. the current directory. The value of the classpath environment variable overrides this value.
If the java command is issued with –cp or –classpath option, it overrides the default ‘.’ and classpath environment variable value.
Below is an example for setting a classpath during class execution
C:>java -classpath "." com.abc.example.SayHello
As opposed to compiling where you need to give exact path, to run the class file, we need to follow the package structure.
This is due to the way the Classloader tries to resolve the class location by combining its package and class name. You must be on the package root location and issue the java command specifying the package structure.
C:>java com.abc.example.SayHello
Hello!!
First, you need to compile java files:
javac /folder/*.java
Then you can run a class with main() function:
java -cp . folder.foobar

Calling Java .jar Program from Perl using System()

The title pretty much says it all, I am trying to get a java program (.jar file) to run from a Perl program. I read another post on Stackoverflow saying this syntax is correct:
system("java filename.jar");
However this is giving me the following error. I'm not sure if the problem is that it shows the filename as "filename/jar" instead of "filename.jar"
Exception in thread "main" java.lang.NoClassDefFoundError: filename/jar
Caused by: java.lang.ClassNotFoundException: filename.jar
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)
You need to call it as java -jar filename.jar.
If you leave out the -jar, then java thinks you're trying to run a class named filename.jar, which it tries to load from the filename/jar file. Since you don't have that file, java throws the error that you see.
You have to use the -jar option to launch a jar.
java [ options ] -jar file.jar [ argument ... ]
Resources :
sun.com - java

NoClassDefFoundError without any class name

I am trying to run a java task from ant. I am trying to run the "org.apache.tools.ant.launch.Launcher" class. I keep on getting the "NoClassDefFoundError" without any class name being specified. I am also getting a "ClassNotFoundException" along with that displaying a message "Could not find the main class: . Program will exit". Here's a snippet of the error
[java] Exception in thread "main" java.lang.NoClassDefFoundError:
[java] Caused by: java.lang.ClassNotFoundException:
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
[java] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
[java] Could not find the main class: . Program will exit.
[java] Java Result: 1
Now I am trying to run an ant class from an ant jar and i specifiy the classpath where this class file resides using the "classpathref" attribute, however I still get this message. I checked the ant jar to check the Manifest and the "main" class is specified properly (it's "org.apache.tools.ant.launch.Launcher") . I have exhausted all my resources. Please help ! ! !
ps: My environment is Eclipse on Ubuntu 9.04
Most likely your classpath is misconfigured.
At a minimum the CLASSPATH should include:
ant.jar and ant-launcher.jar
jars/classes for your XML parser
the JDK's required jar/zip files
(from the ant manual)
Also you seem to be relaunching ant in the current directory (executing the same build.xml). Maybe you'll want to set the "dir" property.
It looks like the Ant task is trying to run Java, but is somehow passing an empty string to the JVM as the name of the class to run. I can get the same stacktrace if I run the JVM directly with a quoted empty string:
C:\>java ""
Exception in thread "main" java.lang.NoClassDefFoundError:
Caused by: java.lang.ClassNotFoundException:
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: . Program will exit.
(This is on Windows, but I don't think that makes much of a difference.)
I can only suggest following up on Alexander Pogrebnyak's comment to akf's answer. Perhaps the webtest.lib property has spaces in it?
Also, is there a good reason for calling ant directly via java, rather than using the ant task?
https://blogs.oracle.com/sreekanth/entry/java_lang_noclassdeffounderror_org_codehaus
java.lang.NoClassDefFoundError: org/codehaus/plexus/classworlds/launcher/Launcher
By sreekanth on Nov 23, 2010
Recently when I am trying to run some Maven scripts, I am getting this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/plexus/classworlds/launcher/Launcher
Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.classworlds.launcher.Launcher
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: org.codehaus.plexus.classworlds.launcher.Launcher. Program will exit.
After spending some time trying various combinations , I found that this is because I have both M2_HOME and M3_HOME set in my environment variables.Once I removed M2_HOME from my environment variables, I could get this working back again.May be this could save some serious time for some one.
This can be a misleading error that is not actually about a class missing from the classpath. If you are using Tomcat it can be due to missing conf files in $CATALINA_BASE/conf
It could also be a misconfigured ant installation, please check your JAVA_HOME and ANT_HOME env variables or try another ant installation.
Ant launcher expects the following params
java -Dant.home=c:\ant org.apache.tools.ant.launch.Launcher [options] [target]
I am affraid we can't proceed answering you if you don't paste your whole build.xml file.
Just try to give your full sample as below:
<java
classname="org.apache.tools.ant.launch.Launcher"
fork="true"
failonerror="true"
dir="${sub.builddir}"
timeout="4000000"
taskname="startAnt"
>
<classpath>
<pathelement location="${ant.home}/lib/ant-launcher.jar"/>
</classpath>
<arg value="-buildfile"/>
<arg file="${sub.buildfile}"/>
<arg value="-Dthis=this"/>
<arg value="-Dthat=that"/>
<arg value="-Dbasedir=${sub.builddir}"/>
<arg value="-Dthe.other=the.other"/>
<arg value="${sub.target}"/>
</java>
This would be extremely helpful to provide you with a possible misunderstanding.
Hope this helps,
Ernani
From this line:
[java] Could not find the main class: . Program will exit.
it looks as though your call to java.exe is finding a . where it expects a class name. Perhaps you are trying to indicate the classpath on the commandline but are neglecting to preface that with the -cp or -classpath flag.
When in doubt, invoke ant -v and watch all your variable declarations, and the whole commandline sent to Java.
Certain path-like quantities are eagerly evaluated, while others are lazily evaluated. I've had plenty of problems where I used one of the former, when my Ant script intended to create a jar that would be used by a later task. Then by the time I invoked the call, it had already pruned my jar from the classpath.
If I had to make a wild guess, I'd bet your commandline looked something like:
java ... -classpath org.apache.tools.ant.launch.Launcher
instead of
java ... -classpath foo.jar;bar.jar org.apache.tools.ant.launch.Launcher
like you expected
I had a similar problem recently. The culprits were 2 tags under the java task, that didn't have their values set, so they resulted in 2 empty command arguments and in the end in 2 spaces in the command line.
For some reason Unix doesn't handle it right. Both Red Hat 5 and Ubuntu displayed the same error. It was OK on Windows 7.
Setting those arguments to have default dummy values solved the issue.

How to invoke groovy with 'java' from command line

I have to ship some groovy code to some users that have only java installed (no grooy, no $groovy_home, etc). I'm trying to invoke groovy from the commandline but I'm having no luck. Here's my bat file:
java -classpath .;lib;bin;bin-groovy introspector.AclCollector
And here's my exception:
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 12 more
Could not find the main class: introspector.AclCollector. Program will exit.
Does anyone have a clue? I have 'groovy-all-1.6-beta-1.jar' in \lib dir.
You have here another example of Groovy app called from Java (in this case, from ant, but the general idea is the same).
java -cp [...];%GROOVY_HOME%/embeddable/groovy-all-1.5.4.jar;[..]
As mentioned by frankowyer, you have the exact groovy jar explicitly listed on the classpath arguments of the java.
Since your clients do not have special environment variable, just replace the %GROOVY_HOME%/... with the complete path to:
groovy.jar or
goovy-all-VERSION.jar (to minimize any conflict with other libraries)
I think you need to explicitly list the groovy jar in the classpath
One way to avoid problems with different class paths on different machines would be to bundle all the necessary dependencies into one single jar, this would also make distribution to users easier. This can be done with this 'GroovyWrapper' script. The default jars (embeddable/groovy-all-.jar and lib/commons.jar) are included by default in the script and if you require other JARS they can easily be added.
See http://docs.codehaus.org/display/GROOVY/WrappingGroovyScript for the full script and instructions.
Here's an example of how to use GroovyWrapper:
Say you have groovy script HelloWorld.groovy, use GroovyWrapper for building HelloWorld.jar, as follows:
$ groovy GroovyWrapper -c -m HelloWorld
GroovyWrapper will compile the script HelloWorld.groovy to HelloWorld.class, and creates a self-executable jar HelloWorld.jar.
Now you can use the HelloWorld.jar for launching the HelloWorld script, simply by running:
$ java -jar HelloWorld.jar
GREAT ANSWER by VonC:
....... Since your clients do not have special environment variable,
just replace the %GROOVY_HOME%/... with the complete path to:
groovy.jar or
goovy-all-VERSION.jar (to minimize any conflict with other libraries)........
this is for windows Vista:
In System Properties | Environmental Variables | User variables:
Variable Name GROOVY_HOME set to C:\groovy\Groovy-1.8.5
and the Variable Name CLASSPATH value's set to include .;%GROOVY_HOME%\embeddable\groovy-all-1.8.5.jar
Don't make the mistake I did(spinning my wheels a bit), of not prep-ending the path of the 'groovy-all' with ".;" (dot and semi-colon): as the path needs the base folder you're in for finding the main class(strange as it seems)
Watch out of [~]!
java -cp .:~/path-to-groovy-all.jar YourClassName # does not work
java -cp ~/path-to-groovy-all.jar:. YourClassName # works
java -cp .:/full/path/to/goovy-all.jar YourClassName # works
In first line tilde is not processed by bash, and java can not understand it.
In second line tilde is replaced by bash and everything works fine.

Categories