I'm new to Java and I've looked around the web for solutions but none seem to work. Please help me.
I have two files. One of them is the java file that contains the main function. In it:
...
VaporVisitor visitor = new VaporVisitor();
...
With that command, I want to create a new object VaporVisitor
, which is a class in a separate file called VaporVisitor.java
. However Java doesn't recognize what VaporVisitor is, presumably because it doesn't know VaporVisitor.java
exists (it's in the same directory). I tried making them part of the same package, in different packages and importing...and all of them failed. Can anyone give me any guidance?
Thanks!
EDIT: Here's exactly what I'm doing, and the error message I get: So I have 3 files. V2VM (with my main function), VaporVisitor, and a provided jar file that has several custom classes. The jar file isn't giving me any problems; it's trying to get java to recognize VaporVisitor.
So in V2VM (the main function), I've tried putting in: import V2VM.java;
which didn't work. I've tried putting V2VM in a subfolder called vv, added package vv;
to VaporVisitor and put in V2VM.java import vv.*
but that didn't work either.
For compiling, I tried javac -classpath [the jar file].jar V2VM.java
The errors it gives me:
V2VM.java:15: cannot find symbol
symbol : class VaporVisitor
location: class V2VM
VaporVisitor visitor = new VaporVisitor();
^
V2VM.java:15: cannot find symbol
symbol : class VaporVisitor
location: class V2VM
VaporVisitor visitor = new VaporVisitor();
^
When I run javacc
I am in the same directory as V2VM, which is also where the other two files are located. I've tried putting V2VM and VaporVisitor in the same package, but that didn't work either. So they are not part of any package now...
EDIT 2: SOURCE CODE OF VaporVisitor and V2VM
V2VM.java:
package vv; //whether I put this or not, it doesn't work
//this stuff was provided, and is related to importing contents of the jar file; don't think this is the problem
import cs132.util.ProblemException;
import cs132.vapor.parser.VaporParser;
import cs132.vapor.ast.VaporProgram;
import cs132.vapor.ast.VBuiltIn.Op;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
import vv.VaporVisitor; //whether I put this or not, it doesn't work
public class V2VM{
public static void main(String [] args){
VaporProgram vp = parseVapor(System.in, System.err);
VaporVisitor visitor = new VaporVisitor();
for(int i=0; i<vp.functions.length; i++){
for(int j=0; j<vp.functions[i].body.length; j++){
vp.functions[i].body[j].accept(parameter, visitor);
}
}
}
public static VaporProgram parseVapor(InputStream in, PrintStream err){
...
}
For VaporVisitor.java:
package vv;
public class VaporVisitor extends VInstr.VisitorPR<Parameter_Type, Return_Type, RuntimeException>{
....
}
All 3 files are in the same directory vv
OK. So first of all, you never refer to a class in Java by appending .java
to its name. Second: in order to compile a class A that uses another class B, the class B must be compiled, and available in the classpath. Or you must compile both A and B at the same time.
So, you should have the following structure:
project
lib
someFile.jar
src
vv
V2VM.java
VaporVisitor.java
classes
Both classes should start with:
package vv;
There's no need to import V2VM in VaporVisitor or vice versa, since they are in the same package.
To compile the files, you should be in the project directory, and use the following command:
javac -cp lib/someFile.jar -d classes src/vv/V2VM.java src/vv/VaporVisitor.java
This will compile the two files at once, and put their compiled class files in project/classes:
project
classes
vv
V2VM.class
VaporVisitor.class
You put the jar file in the classpath because the classes you compile use classes from this jar file.
Then, to run your class, you need to have both the jar file, and the classes directory in the classpath. And the fully qualified name of the main class is vv.V2VM
. So the command is
java -cp lib/someFile.jar:classes vv.V2VM
If you're on windows, you must use \
instead of /
, and ;
instead of :
.
If you wanted to compile VaporVisiotr first, and then V2VM, you could. But then you would have to do the following:
javac -cp lib/someFile.jar -d classes src/vv/VaporVisitor.java
This compiles VaporVisiotr only, and put its class file in project/classes
. Then you need to compile V2VM, which depends on VaporVisitor. So the compiled VaporVisitor class must be available in the classpath. So the command is
javac -cp lib/someFile.jar:classes -d classes src/vv/V2VM.java
If you decided to put VaporVisitor in another package (vv.foo
for example), then you would need the following structure:
project
lib
someFile.jar
src
vv
V2VM.java
foo
VaporVisitor.java
classes
The VaporVisitor.java would need to start with
package vv.foo;
And the V2VM.java file would need to have
package vv;
import vv.foo.VaporVisitor;
Read the tutorial about packages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With