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.
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?
This question already has answers here:
Is the Main method must needed in a Java program?
(9 answers)
Closed 7 years ago.
Is there a class called 'main' or something that java looks for in your project?
If it finds it, does the compiler take the 'main.class' as a starting point for compilation?
I don't know what to call my main class, whether there is even such a thing, or if more than one class act in tandem to control how the program works &/or call all the other classes.
I have a little experience in programming Minecraft mods, but that is also a bit scanty, so it would be awesome if you guys helped me out =]
When the JVM starts it will run a method named main with the signature:
public static void main(String args[])
When you start the JVM you tell it which class to use to find the main method. Alternatively you can start the JVM with a jar file, in which case it will look at the manifest in order to determine which class's main method to use.
For the jar method you execute the JVM like java -jar myjar.jar and define the main class in the manifest file with the Main-Class attribute.
Otherwise you start the JVM like java com.package.MyMainClass
At compilation time, it doesn't usually matter - you just specify all the source files you want to compile. (You can specify a subset and the compiler will find other source files it needs, but it's better to specify everything you want compiled.)
When you execute a Java program from the command line, you specify the class containing the main method:
java com.foo.MyAwesomeApp
In runnable jar files, there should be META-INF/MANIFEST.MF file with Main-Class specified.
Maybe this question may be splitting hairs, but when I compile a file from command line like :
javac MyClass.java
then afterward I cannot run it by saying
java MyClass.class
I have to call:
java MyClass
What is the motivation for this notation?
Because you run a class on the classpath, which may be contained inside a jar for example. You couldn't use your syntax in that case.
Java compiler needs a compilation unit; this is by default (at least) a java source file, with the whole of classes defined in it and its dependencies.
Java interpreter (the jvm) needs a single class with a main method as entry point of the execution - it must start somewhere.
You'd have to ask Sun (now Oracle) for the development history, but I do want to point out that for folks who are just using Java rather than developing Java, "java DoSomething" is easier to remember, and to type, than "java DoSomething.class"
There is no way to run a Java program that is not a class. For that reason, there is no reason to mandate typing the ".class". You might also invoke a class from within a JAR on your path, or directly, but it's still instantiating a class (possibly a "default" class from the Manifest).
Because the name of the class is MyClass and not MyClass.class. And when running java you specify the CLASS NAME and not the PATH to the actual compiled file.
For more in depth knowledge I guess Sun & Oracle will have to answer :)
Imagine that you have a class named package and you have a class named Class, in a package named package,
--CurrentFolder
--package
Class.class
package.class
so executing java package.class may lead to an undecidability to the compiler!
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'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