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

﹥>﹥吖頭↗ 提交于 2019-12-07 15:22:25

问题


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


回答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?




回答2:


Try doing

javac TestShipment.java
java TestShipment
pause



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/21244748/issue-running-java-program-from-batch-file-runs-fine-in-ide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!