Java Cannot Find Class File - java

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

Related

How to specify which implementation to test

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

Issue running java program from batch file, runs fine in IDE

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

How can I make my java app see the class it needs?

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.

Java code not compiling with 'javac' but compiles in Eclipse

I have just written a Java multi-threaded program in Eclipse. It compiled fine and works like a charm.
However, as this is coursework we are required to ensure that it compiles in the command line using 'javac' otherwise we score ZERO!
So, some classes compile others don't. The error I'm getting is the following ( they are all similar just with different class names, this is one example)
GateRunnable.java:7: cannot find symbol
symbol : class Station
location: class package.name.here.GateRunnable
public GateRunnable(Station st) {
^
Is this a javac issue? Any help appreciated.
Your compile -classpath and/or -sourcepath is incomplete. The compiler doesn't know where to find class Station. Here is a related question that describes how to set the classpath to include all the classes you want.
To solve the problem I was having, it was simply necessary to compile all classes by using the following command:
javac *.java
which compiles all java files in the directory.
have you compiled every .java file in your folder/packages? if not, then do so. Eclipse usually does that for you, but within the terminal it's you taking the responsibility of compiling every part of the code.

How can I tell javac how to find the imageio-classes?

I'm new to java development, I just want to use javac for my build system. I'm using java to add a feature to a program someone else wrote, specifically involving GeoTiff images.
I found a class online that I would like to use, however I'm having trouble building the class, no matter what I do I get this message:
javac GeoTiffIIOMetadataAdapter.java
GeoTiffIIOMetadataAdapter.java:11: package com.sun.media.imageio.plugins.tiff does not exist
import com.sun.media.imageio.plugins.tiff.GeoTIFFTagSet;
I'm on RHEL5, so I installed the package I thought I needed, jai-imageio-core.x86_64. But the problem persists. I think that I'm not setting some variable corrently (like -sourcepath or something). I would appreciate any help.
You need to include the jar with -cp or -classpath.
So your compile would be like java -cp "<location to jai_imageio-1.1.jar>" <your java class> .
I think you need this jar file.
You can read more about javac here.
Find out where the package installed the jar file with the class you want to import, and add it to the javac commandline in the -classpath. (You then also need to include it in the classpath when your plugin runs; how to do that may depend on the program it plugs into).
I think that I'm not setting some variable correctly (like -sourcepath or something)
This tutorial briefly introduces the usage of environment variables in Java: PATH and CLASSPATH
This one seems to be the most popular answer to various classpath related questions I've seen at online forums: Setting the class path.
To avoid "blind recommendation" I quickly skimmed through it before adding to this answer and, well... it really covers most of what one needs to know to deal with classpath. Pretty good; the reason why I didn't look into it before is that there always has been some guru nearby who explained stuff to me.

Categories