如何从命令行中的包中有类的闪存驱动器运行java程序? [英] how to run java program from flash drive that has classes in package, from command line?

查看:98
本文介绍了如何从命令行中的包中有类的闪存驱动器运行java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务,我必须在闪存驱动器中提交我的代码。

I have an assignment where I have to submit my code in flash drive.

我在eclipse中完成所有编码并获得所有输出。我尝试通过命令行运行它,它的工作原理。我将所有java文件和类文件复制到闪存驱动器,并通过命令行再次尝试它在我的笔记本电脑中工作。但是,当我进行潜水并将其放入台式计算机并通过命令行运行时,它无法识别包中的类。

I did all coding in eclipse and getting all output. I tried running it through command line and it works. I copied all java files and class files to flash drive and tried again through command line it works in my laptop. but when I take that dive and put it my desktop computer and run through command line, it can not recognise the classes that are in the package.

我有一个包a1 .cis568在这个包下我有几个类。主要类是 A1 ,其他类是 Circle Point PlaneCircle EHashtable CHashtable 在同一个包中。

I have a package a1.cis568 under this package I have several classes. main class is A1 and other classes are Circle, Point, PlaneCircle, EHashtable, CHashtable in the same package.

我必须在命令行上使用以下行来编译并通过闪存驱动器运行我的程序,(这样做时不必更改任何类路径)

I have to use following line on command line to compile and run my program through flash drive, (don't have to change any classpath while doing so)

E:>        
E:>javac -d . A1.java        
E:>java a1.cis568.A1

当我搜索对于解决方案,我发现我可以在闪存驱动器上安装JDK并运行代码。我尝试安装JDK但它无法解决我的问题。

When I was searching for solution I found that I can install JDK on flash drive and run the code. I tried installing JDK but it doesn't solve my problem.

推荐答案

您似乎在编译源代码时遇到麻烦!

You seem to have troubles compiling your source code!

由于您将包层次结构导航到 A1.java 所在的位置,然后尝试编译,编译器无法找到 A1 的其他类依赖于哪些类,哪些不在同一目录中。你必须告诉它在哪里找到它们,这是使用 javac 的选项 -sourcepath 完成的。

Since you navigate trought the package hierarchy to where A1.java is located and then try to compile, the compiler cannot find other classes which A1 depends on and which are not in the same directory. You have to tell it where to find them, this done using javac's option -sourcepath.

因此,从类 A1.java 的同一位置,您可以使用以下内容编译代码:

So from the same location of the class A1.java you could compile your code using something like this:

E:> javac -d . A1.java -sourcepath ..\..\

这里。 .\..\ 用于告诉源代码可以找到实际directroy之上的两个目录(基于你的包树 a1 \cis568 \ )。

Here ..\..\ is used to tell that the sources can be found two directories above the actual directroy (based on your package tree a1\cis568\).

但为了方便起见,我建议您从源代码树的根目录进行编译。在这种情况下, sourcepath classpath 是默认的实际工作目录。

But to make things easy I would suggest you compile from the root of you source code tree. In this case the sourcepath and classpath are the actual working directory per default.

因此,在commad提示符下导航到package /目录 a1 所在的位置,然后像这样编译:

So navigate in the commad prompt to the location where the package/directory a1 is, then compile like this:

E:> javac -d . a1\cis568\A1.java

comipiler应该能够找到所有 A1 依赖于(假设它们都存在于源代码根目录中的正确包中)。

The comipiler should be able to find all classes on which A1 depends on (assuming they all exist with the correct package inside the root of your source code).

使用

E:> java a1.cis568.A1

这篇关于如何从命令行中的包中有类的闪存驱动器运行java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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