Ioanna Katsanou Ioanna Katsanou - 1 year ago 71
Java Question

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. I am using Windows10.
The problem is the following. I have a folder named "folder1", which is located o the dekstop of my computer. So the full path would be
Inside that folder I have created a second folder which I named folder2. so the path to this would be

Inside folder2 I have a java file named
I want to compile it and run this file with setting the -classpath option through cmd. I dont want to set the path or to add the folder to tha path from environment variables.

I am trying

C:\Users\Ioanna\javac -cp C:\Users\Ioanna\Desktop\folder1\folder2

but it says file not found.
I tried several other alternatives, but I can't seem to find how to compile successfully the program.

Answer Source

Code compilation (to bytecode) and code execution are two separate steps, in Java.

First, compile your .java to obtain the corresponding .class file (I'm assuming your folder paths are right):

C:\Users\Ioanna\javac C:\Users\Ioanna\Desktop\folder1\folder2\

This will give you example.class in that same folder.
Next, run that class (provided it has a main() method):

C:\Users\Ioanna\java -cp C:\Users\Ioanna\Desktop\folder1\folder2 example
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download