Java项目在Eclipse上运行时,在使用批处理时出现错误。

huangapple go评论72阅读模式
英文:

Java project running on eclipse but giving error while using batch

问题

我为我的大学课程制作了一个项目,在这个项目中我需要传递命令行参数。在Eclipse上运行得非常好,但当我使用批处理文件来运行它时,出现了以下错误:

Java项目在Eclipse上运行时,在使用批处理时出现错误。

我的批处理文件如下所示:

set path="c:\Program Files\Java\jdk-14.0.2\bin";
javac FileHand.java
java FileHand DirectBuffer 1024 Sample.txt
pause
英文:

I made a project for my uni. where I need to pass command line arguments. It is running perfectly fine on eclipse but when I run it using a batch file.

Java项目在Eclipse上运行时,在使用批处理时出现错误。

my batch file looks like

set path = "c:\Program Files\Java\jdk-14.0.2\bin";
javac FileHand.java
java FileHand DirectBuffer 1024 Sample.txt
pause

答案1

得分: 1

不要完全设置%path%。如果您想要将完整路径“硬编码”到Java,则可以这样做;编写C:\program files\....\javac,或者SET JAVA_LOC=...然后%JAVALOC%\javac。但是,显然这是不必要的;您搞乱了SET PATH语句,但javac正在被调用,因此,您可能只需删除整个“set path”行。

问题出在类路径上。有一个名为DirectBuffer.class的文件。它在某个地方 - 您说过“在eclipse中可以工作”,这意味着eclipse可以找到此文件,因为您告诉它在哪里。您需要告诉javac它在哪里。您可以这样做:

javac -cp LOC1;LOC2;LOC3 FileHandjava

java -cp .;LOC1;LOC2;LOC3 FileHand DirectBuffer 1024 Sample.txt

其中LOC1是一个路径。它可以是目录,也可以是一个jar文件。您的问题没有明确说明这一点,但假设DirectBuffer位于com.foo.pkg包中(因此,在您的源文件中有import com.foo.pkg.DirectBuffer;),那么:

要么:

cd(无论您为LOC1放置在什么地方)
cd com\foo\pkg
dir

应该打印出,除其他内容外,“DirectBuffer.class”,或者,如果LOC1是一个jar文件:

jar tvf(在LOC1中列出的jar文件)

应该打印出com/foo/pkg/DirectBuffer.class,以及其他内容。您已经告诉过eclipse这一点,因此现在找到那些您这样做的地方,并告诉javac。

英文:

Do not set %path% at all. If you want to 'hardcode' the full path to java, then do so; write C:\program files\....\javac, or SET JAVA_LOC=... and then %JAVALOC%\javac. But, this is clearly is not needed; you messed up your SET PATH statement and yet javac is being invoked, so, you should probably just remove the entire 'set path' line.

The problem is classpath. There is a file named DirectBuffer.class. It is somewhere - you said that 'it works in eclipse', which means eclipse can find this file, because you told it where it is. You need to tell javac where it is. You do this as follows:

javac -cp LOC1;LOC2;LOC3 FileHandjava

java -cp .;LOC1;LOC2;LOC3 FileHand DirectBuffer 1024 Sample.txt

where LOC1 is a path. It can be a directory, or a jar file. Your question does not make this clear, but let's say DirectBuffer' is in the com.foo.pkgpackage (so, you haveimport com.foo.pkg.DirectBuffer;` in your source file), then:

either:

cd (whatever you put for LOC1)
cd com\foo\pkg
dir

should print, amongst other things, 'DirectBuffer.class', or, if LOC1 is a jar file:

jar tvf (the jar file listed in LOC1)

should print com/foo/pkg/DirectBuffer.class, amongst other things. You've already told eclipse this, so now find those places where you did that and tell javac about it.

huangapple
  • 本文由 发表于 2020年9月23日 22:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64029965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定