I am using the code from Rome's tutorials page http://wiki.java.net/twiki/bin/view/Javawsxml/Rome05TutorialFeedReader . Also trying this one: http://wiki.java.net/twiki/bin/view/Javawsxml/Rome05TutorialFeedReader
Compiling works, but I'm not sure how to run these examples. Why I just type java FeedReader or java FeedAggregator into the command line, I get the error:
C:\projects\freshmeat\src>java FeedAggregator http://freecode.com/?format=atom
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/syndication/f
eed/synd/SyndFeed
plus the large block that follows this error
Why is this happening, how do I fix it and try these things out? How do I get something to work with Rome!?
You need to include rome in the runtime classpath (in addition to the compile-time classpath)
java -classpath lib/rome.jar FeedAggregator ...
The samples you are trying to run are in the package com.sun.syndication.samples. You say you are a complete beginner, so, to make things simpler, I would recommend that you remove the line beginning with package in each of FeedReader.java and FeedAggregator.java. Recompile the classes after removing their package directives.
Then, to run these classes, make sure you're in the same directory as the class files FeedReader.class and FeedAggregator.class that javac created. Then, try running:
java -cp c:\projects\freshmeat\libs\rome-1.0.jar;c:\projects\freshmeat\libs\jdom-1.0.jar;. FeedReader
(and similarly for FeedAggregator.)
Note also that I've added the current directory, ., to the -cp attribute. Without this, the Java virtual machine won't know that it has to look in the current directory to find your FeedReader and FeedAggregator classes.
If you were to reinstate the package directives, you'd find the class files FeedReader.class and FeedAggregator.class would be created inside a directory com\sun\syndication\samples when you compile their sources. To run the class files from this location, you'd use a command line such as
java -cp c:\projects\freshmeat\libs\rome-1.0.jar;c:\projects\freshmeat\libs\jdom-1.0.jar;. com.sun.syndication.samples.FeedReader
and you'd run this from the directory containing the com subdirectory, not the directory that contains the class files.
More information on packages in Java can be found here.
Related
I have a few classes in one java package. Actually, I'm using eclipse, and compiling works just fine since eclipse takes care of it. But now that I'm using the command line, compiling does not work and the error is that I am referring to class B inside of class A, and the compiler does not recognize class B.
I have done some research, and people say to use the -cp flag to allow the compiler to look for the other classes file. Specifically, the command I am using is java -cp . UDPClient.java. This returns the same error that the other class cannot be found.
Why is this and what should I do?
I am currently trying to include the jnativehook library and test out one of its examples.
I downloaded the .jar file from its website and created a .java file which has an example program. These are in the same folder.
I have followed other questions on here which have tried to tell me how to compile and run the program. I have tried:
javac -classpath jnativehook-2.1.0.jar GlobalKeyListenerExample.java
java -classpath jnativehook-2.1.0.jar GlobalKeyListenerExample
It compiles fine, however when I enter the second command to run it I get:
Error: Could not find or load main class GlobalKeyListenerExample
Caused by: java.lang.ClassNotFoundException: GlobalKeyListenerExample
All I want to do is be able to include a .jar file in my project and compile it from the command line. I am not using eclipse at the moment because I am getting too many problems with it and I also want to learn to program without IDEs.
(I am on windows by the way). Also, please do not mark this as duplicate. If it was actually a duplicate then I would be able to find a solution to my problem from the other similar questions.
Following the documentation of javac, especially "Example 3 - Specify a User Class Path" (at the bottom of the page), your java file has to have a package:
package com.example;
public class GlobalKeyListenerExample {
public static void main(String ... args) {
}
}
Then that file has to lie in the corresponding directory structure:
..something/com/example/GlobalKeyListenerExample.java
Now you can call the compiler with
javac -classpath jnativehook-2.1.0.jar;...something com/example/GlobalKeyListenerExample.java
And after compiling you can start it with
java -classpath jnativehook-2.1.0.jar;...something com.example.GlobalKeyListenerExample
I have deploy my java code on AWS cloud, when I compile it on windows through terminal, I just have to use the command.
javac mainApp.java
it automatically create .class files of all other classes, lets say I have another class
class newProcessClass
who's variable is in mainApp.java, on EC2 when I compile it, it is giving me the error
error: could not find this symbol
newProcessClass npc = new newProcessClass();
same for the other classes. how can I compile it, and run it.
According to your description, I think that might be caused by classpath issue,
So please make sure current path(.) is under your classpath.
You can do this by type the follow into your terminal(pay attention to the little dot please!)
export CLASSPATH=.;$CLASSPATH
If you would like to persistent this setting and avoid set this everytime, you'd better add it to your .bashrc file.
And then when you run the class via java command, please also specify classpath as below
java -cp . mainApp
I recommend use Maven for the life cycle for application java and when you compiled the mainApp.java this action has that compile all file. you can get me more information of error stack?. For other side I think that is best that you use Maven. Best regards
I'm doing some basic java homework for a class on my new laptop - issue is, I can't seem to get the program to compile and run from my batch file using the directions the instructor gave me.
I've set the Path variable to my JDK inside the Environment Variables settings.
My program is a simple shipping program to keep track of shipment information - I have the program working flawlessly in NetBeans (which our instructor advised us to use for developing the code), but he's going to be testing them using batch files, so we're also advised to test them on our systems with one we create prior to turning them in - pretty straightforward.
Issue is, I cannot seem to get this to work. I've never done it before, but I've used .bat files to compile and run C++ programs, as well as using makefiles on a unix system, so I feel like I'm absolutely stupid for not figuring this out on my own, but none of my searches have returned any fruitful solutions that help at all.
My program consists of 3 .java files:
Shipment.java - an interface that contains abstracted methods that are implemented in the ShipmentHW1 class
ShipmentHW1.java - a class that implements the abstracted methods from Shipment and has constructors, etc to create a usable object
TestShipment.java - the main class of this program, which utilizes and creates ShipmentHW1 objects based on preset parameters. This is super duper basic stuff here, and again, it runs perfectly fine inside the NetBeans IDE.
The instructions given to us state to have the batch file inside the package directory (which in this case I've set aside a seperate folder on my desktop titled "shipping", which is the package name - shouldn't be any issues there), where the 3 .java files are located as well.
They say if you don't need to explicitly list the path to the JDK, then you can simply have
javac TestShipment.java
java TestShipment.java
pause
Afterwards I get errors talking about how it "cannot find symbol Shipment s = new ShipmentHW1();"
I've tried adding imports, but since they're in the same package it shouldn't even be an issue.
Directory path is
C:\Users\X\Desktop\shipping
All 7 files are contained within:
TestShipment.java
TestShipment.class
Shipment.java
Shipment.class
ShipmentHW1.java
ShipmentHW1.class
doHW1.bat
Does anyone have any idea? I can provide more information if I've been too vague
Also, I'm on Windows 8 if that makes any difference
Solved
Batch file now reads
javac TestShipment.java Shipment.java ShipmentHW1.java
cd ..
java shipment.TestShipment
pause
and it works like a charm. Anyone have any ideas why I had to call the package.class instead of just compiling it regularly?
Try doing
javac TestShipment.java
java TestShipment
pause
Without seeing the contents of TestShipment.java, I'll assume you have some dependency on the Shipment and ShipmentHW1 classes. As such, when you execute a program that uses the TestShipment class, you need to have the .class files for each of the three (and any other dependencies).
So you will have to compile Shipment.java and ShipmentHW1.java as well before running your java command. If they are in the same package, you're good, if not, you will have to specify an appropriate value for the -cp option.
When running java with a class name, you need to specify the fully qualified class name.
If your .java files are declared to be in the 'shipping' package, then you probably need to be running java from the parent directory of 'shipping', e.g.
cd <path>/shipping
javac TestShipment.java
cd ..
java shipping/TestShipment
I am trying to run a java program on my AIX server. I wrote the classpath
/usr/java6/bin/javac -classpath :.:/usr/jdk/commons-net-3.0.1.jar:/usr/jdk/classes12.jar:/usr/jdk/mysql-connector-java-5.1.17-bin.jar:/usr/jdk/jtds-1.2.5.jar:/urs/jdk/mail.jar:/usr/jdk/joda-time-1.6.2.jar:/usr/jdk/commons-codec-1.4.jar:/usr/jdk/commons-logging-1.1.1.jar:/usr/jdk/httpclient-4.1.1.jar:/usr/jdk/httpclient-cache-4.1.1.jar:/usr/jdk/httpcore-4.1.jar:/usr/jdk/httpmime-4.1.1.jar:/usr/jdk/mailapi.jar:/usr/jdk/pop3.jar:/usr/jdk/smtp.jar:/usr/jdk/dsn.jar:/usr/jdk/imap.jar -d . *.java
which seems to compile correctly. Then when I run the program
java daily_transmission
i get the error java.lang.NoClassDefFoundError: javax.mail.Address. All the .jar files that were in Eclipse when it worked before are in the /usr/jdk file.
A NoClassDefFoundError means that (quoting the relevant JavaDoc):
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.
Two things to check:
Firstly, you appear to have have a typo in your classpath (though it can't have been there when you compiled the code, or it wouldn't have compiled successfully, so that's clearly not the root cause of the exception you're seeing.):
/urs/jdk/mail.jar
should be
/usr/jdk/mail.jar
Make sure that you are also using the -classpath option to pass the classpath to java as well as to javac to pick up those .jar files at runtime:
java -classpath :.:/usr/jdk/commons-net-3.0.1.jar:/usr/jdk/classes12.jar:/usr/jdk/mysql-connector-java-5.1.17-bin.jar:/usr/jdk/jtds-1.2.5.jar:/urs/jdk/mail.jar:/usr/jdk/joda-time-1.6.2.jar:/usr/jdk/commons-codec-1.4.jar:/usr/jdk/commons-logging-1.1.1.jar:/usr/jdk/httpclient-4.1.1.jar:/usr/jdk/httpclient-cache-4.1.1.jar:/usr/jdk/httpcore-4.1.jar:/usr/jdk/httpmime-4.1.1.jar:/usr/jdk/mailapi.jar:/usr/jdk/pop3.jar:/usr/jdk/smtp.jar:/usr/jdk/dsn.jar:/usr/jdk/imap.jar daily_transmission
NoClassDefFoundError means that the ClassLoader was able to find the class, but it was not able to be loaded. This usually happens when some static field in the offending class could not be initialized. So I am sure that your classpath might be correct; but some static field in that class could not be set correctly.