从批处理文件运行 java 程序的问题,在 IDE 中运行良好 [英] Issue running java program from batch file, runs fine in IDE

查看:24
本文介绍了从批处理文件运行 java 程序的问题,在 IDE 中运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的新笔记本电脑上为一门课做一些基本的 Java 作业 - 问题是,我似乎无法使用讲师给我的指示从我的批处理文件编译和运行程序.

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.

我已在环境变量设置中将 Path 变量设置为我的 JDK.

I've set the Path variable to my JDK inside the Environment Variables settings.

我的程序是一个简单的运输程序,用于跟踪运输信息 - 我的程序在 NetBeans 中完美运行(我们的讲师建议我们使用它来开发代码),但他将使用批处理文件对其进行测试,因此,我们还建议在上交之前使用我们创建的系统在我们的系统上测试它们 - 非常简单.

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.

问题是,我似乎无法让它发挥作用.我以前从未这样做过,但我已经使用 .bat 文件来编译和运行 C++ 程序,以及在 unix 系统上使用 makefile,所以我觉得我没有自己解决这个问题是绝对愚蠢的,但我的搜索都没有返回任何有帮助的富有成效的解决方案.

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.

我的程序由 3 个 .java 文件组成:

My program consists of 3 .java files:

Shipment.java - 包含在 ShipmentHW1 类中实现的抽象方法的接口

Shipment.java - an interface that contains abstracted methods that are implemented in the ShipmentHW1 class

ShipmentHW1.java - 一个类,它实现了 Shipment 的抽象方法并具有构造函数等来创建一个可用的对象

ShipmentHW1.java - a class that implements the abstracted methods from Shipment and has constructors, etc to create a usable object

TestShipment.java - 该程序的主类,它根据预设参数利用和创建 ShipmentHW1 对象.这是非常基本的东西,而且它在 NetBeans IDE 中运行得非常好.

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.

给我们的说明说明将批处理文件放在包目录中(在这种情况下,我在桌面上留出了一个名为shipping"的单独文件夹,这是包名称 - 不应该是任何问题),3 个 .java 文件也位于其中.

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.

他们说如果你不需要明确列出JDK的路径,那么你可以简单地

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

之后我在谈论它如何找不到符号 Shipment s = new ShipmentHW1();"时遇到错误我试过添加导入,但因为它们在同一个包中,所以它甚至不应该是一个问题.

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.

目录路径是

C:UsersXDesktopshipping

所有 7 个文件都包含在:

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

另外,如果这有什么不同,我使用的是 Windows 8

Also, I'm on Windows 8 if that makes any difference

推荐答案

已解决

批处理文件现在读取

javac TestShipment.java Shipment.java ShipmentHW1.java
cd ..
java shipment.TestShipment
pause

它就像一个魅力.任何人都知道为什么我必须调用 package.class 而不是定期编译它?

and it works like a charm. Anyone have any ideas why I had to call the package.class instead of just compiling it regularly?

这篇关于从批处理文件运行 java 程序的问题,在 IDE 中运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆