简体   繁体   中英

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', eg

cd <path>/shipping
javac TestShipment.java
cd ..
java shipping/TestShipment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM