My professor has given us an assignment to implement two interfaces and he has built a tester for each interface. I have written my code, but when I run the testers I always get stuck at the line to specify which implementation to test. Here is a link to the website so you can view the testers. I have no idea what to do. If I need to provide anymore information let me know.
https://www.cct.lsu.edu/~sbrandt/csc1351/06/1351-merge-sort.php
In MTester.java and MTesterL.java, you can see the line Class<?> c = Class.forName(args[0]); . That means that you need to pass to the java test program the name of your Implementation. Look how to pass args to main here: https://stackoverflow.com/a/19648592/5947244
When I understand your problem and the tester code currently, you need to pass the full class name of your implementation as the command line argument of the program.
Assuming all the source files are the current directory and all are in the default package (have no package declared on top) and your implementation is in a file MSorter.java, the command line could look like:
> javac -cp . *.java
> java -cp . Tester MSorter
> java -cp . MTesterL MSorterL
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'd like to write a method that is executable from the console
Say for instance I have a class "Plus" that adds 3 to an int "i". I'm supposed to be able to run the program from the console as follows:
$ java Plus 7
10
basically I want to read the value after "Plus" as the int "i" a
I can't find any documentation on the subject, but I wasn't really sure how to search.
It should be possible with the standard java package, since it's homework.
Any help would be appreciated
Thanks!
A good place to start is 1) Look into the main method and parameters it takes, namely, the args[] array. This is where you'll implement the logic to add 3 to some int i 2) Look into loops. 3) Look into executable jars
You need to install jdk and JVM. Set path variable to javac present in bin folder of your jdk.
Then you can use javac program.java to compile and java classpath program args to execute.
Reference: https://docs.oracle.com/javase/tutorial/essential/environment/paths.html
I'm not sure what is going wrong here. I have to write a Tetris program based off of a Skeleton given by my teacher for school. The current class that I am implmenting is called "TetrisPiece" and the abstract class being extended is called "Piece." For some reason I cannot compile my code because it cannot located the Piece class.
I have Piece.java and TetrisPiece.java in the same folder. The structure is:
/src
/TetrisPiece.java
/Piece.java
/Piece.class
I type
javac Piece.java
and it compiles correctly, then I type
javac -cp . TetrisPiece.java
and it results in a compiler error (I have to type -cp . because I messed up my classpath somehow and Java can't find the current directory). I looked through a couple similar StackOverflow Questions and they did not have an answer to this. If the information I provide is not detailed enough (which I assume it isn't) please tell me what else I should provide to give an adequate answer.
You need to compile the files at the same time:
javac Piece.java TetrisPiece.java
Then, assuming TetrisPiece has a main() method, you can run the program with:
java TetrisPiece
I built a little command line program in java. It has a class that gets instantiated and used in the main method. But when I go to compile it at the command line it doesn't 'see' the class it needs to run. It accepts arguments passed in at runtime. I can set it up in Netbeans and it runs beautifully. But I want to be able to use it at the command line. I've tried jar-ing it up, it throws an exception and doesn't see the class that I'm instantiating in main. I took Java in my CS program, but my Prof didn't cover deployment in particular depth.
Any ideas to help me out of my pickle?
Thanks!!
Do either of your classes have packages? If they do, they'll have a first statement of "package ", and it makes a difference.
I'm going to assume that at least your Age class does have a package, I'll call the package 'a'.
Let's further assume a main class of "Alex"; it would have an import statement of "import a.age;".
Let's assume you are in a directory named "george".
Your Alex.java file (without a package statement) needs to be in george. Age.java needs to be in a directory underneath george named a.
You can compile your main file with the command "javac Alex", and can run it with "java Alex".
If you tell us more specifics about your problem, we can be more specific about what you need.
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.