简体   繁体   English

无法使用classpath选项从cmd运行此程序

[英]Cannot run this program from cmd with classpath option

this is a beginner question. 这是一个初学者的问题。 I am having a problem running my java program from command line. 我在从命令行运行Java程序时遇到问题。 I am using Windows10. 我正在使用Windows10。 The problem is the following. 问题如下。 I have a folder named "folder1", which is located o the dekstop of my computer. 我有一个名为“ folder1”的文件夹,该文件夹位于计算机的桌面上。 So the full path would be C:\\Users\\Ioanna\\Desktop\\folder1 Inside that folder I have created a second folder which I named folder2. 因此完整路径为C:\\ Users \\ Ioanna \\ Desktop \\ folder1在该文件夹中,我创建了第二个文件夹,命名为folder2。 so the path to this would be C:\\Users\\Ioanna\\Desktop\\folder1\\folder2 所以它的路径是C:\\ Users \\ Ioanna \\ Desktop \\ folder1 \\ folder2

Inside folder2 I have a java file named example.java I want to compile it and run this file with setting the -classpath option through cmd. 在folder2内部,我有一个名为example.java的Java文件,我想对其进行编译并通过通过cmd设置-classpath选项来运行该文件。 I dont want to set the path or to add the folder to tha path from environment variables. 我不想设置路径或将文件夹从环境变量添加到tha路径。

I am trying 我在尝试

C:\Users\Ioanna\javac -cp C:\Users\Ioanna\Desktop\folder1\folder2 example.java

but it says file not found. 但它说找不到文件。 I tried several other alternatives, but I can't seem to find how to compile successfully the program. 我尝试了其他几种选择,但似乎找不到如何成功编译程序的方法。

java expects the path of the file(s) to compile. Java希望编译文件的路径。 And example.java is not in the current folder (C:\\Users\\Ioanna). 并且example.java不在当前文件夹(C:\\ Users \\ Ioanna)中。

Use 采用

javac Desktop\folder1\folder2\example.java

Code compilation (to bytecode) and code execution are two separate steps, in Java. 在Java中,代码编译(至字节码)和代码执行是两个单独的步骤。

First, compile your .java to obtain the corresponding .class file (I'm assuming your folder paths are right) : 首先,编译.java以获得相应的.class文件(我假设您的文件夹路径正确)

C:\Users\Ioanna\javac C:\Users\Ioanna\Desktop\folder1\folder2\example.java

This will give you example.class in that same folder. 这将在同一文件夹中提供example.class
Next, run that class (provided it has a main() method): 接下来,运行该类(假设它具有main()方法):

C:\Users\Ioanna\java -cp C:\Users\Ioanna\Desktop\folder1\folder2 example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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