3

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

6
  • 2
    Look into the concept of classpath. Commented Jan 20, 2014 at 21:43
  • I've tried this as well: javac -cp "lib*" Testshipment.java java -cp "lib*" TestShipment.java and I haven't seen any noticeable changes. It's as if it cannot find the other files that are sitting in the same directory Commented Jan 20, 2014 at 21:46
  • Can you show us your directory structure and the files in each? Commented Jan 20, 2014 at 22:07
  • C:\users\x\desktop\shipping\<JAVA FILES>. "shipping" is the package, etc, and the .java/.class files are all in there along with the .bat file named doHW1.bat Commented Jan 20, 2014 at 22:08
  • It'll be easier if you edit your question and be a bit more detailed. Commented Jan 20, 2014 at 22:09

4 Answers 4

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

You did just compile it regularly. (Compilation is the "javac" step.) The short answer is classloading. When Java looks for a class, it looks for it starting from each location in your CLASSPATH down the directory tree corresponding to its package - so when a class references another class named "shipment.Shipment", the classloader looks for shipment/Shipment.class under each path in the CLASSPATH. (There's a lot more going on, but that's the quick version) In many configurations (including yours, apparently), CLASSPATH contains the current directory by default.
0

Try doing

javac TestShipment.java
java TestShipment
pause

7 Comments

I went ahead and tried it, got the same issue, but now with an added bonus of a java.lang.NoClassDefFound error: wrong name (shipping\TestShipment) I've already looked through the code multiple times to make sure this isn't just a dumb spelling issue, or an incorrect file path, but everything is as it should be...
After doing javac TestShipment.java, do dir and post what cmd says. You were also doing java TestShipment.java, that makes javac have no point, javac is supposed to compile the .java file into .class file. Try using java TestShipment.class if nothing else works.
dir posts that there are 7 files in the directory - TestShipment.java TestShipment.class Shipment.java Shipment.class ShipmentHW1.java ShipmentHW1.class doHW1.bat my directory path is C:\users\x\desktop\shipping
I see, so the try doing: javac FILE_YOU_WANT_TO_RUN.java and then java FILE_YOU_WANT_TO_RUN.class.
That's what I've been doing. My batch file currently reads javac TestShipping.java java TestShipping pause
|
0

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.

2 Comments

They are indeed in the same package - that's the weird part. The .class files have been generated in the folder, but then the errors pop up. And the errors are telling me it can't find that class, which is preposterous. I'm obviously doing something incorrect, but it's naught to do with the code itself, or where the files are kept. Unless you can't just have them in a folder and run them, but I've never heard of that being an issue
@user It doesn't matter that the .java files are there. The corresponding compiled .class files must be there. Are you saying they are?
0

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

1 Comment

so I should change directory...to shipping? It's already inside shipping, a cd <blah>/shipping would simply put my working directory exactly where it already is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.