Run file from command line windows

How to run a .py file in windows command line?

I have written a simple python program using IDLE to run it from command line. I don’t have permission to save .py file in python directory (C:\program files\python33) so I saved it to C:\Pyscripts.

Also python was already been added to the PATH and I can run a simple print («Hello») in command line. I have saved this line into a py file and Now I want to run it from command prompt but I have faced different errors! I have searched and tested different ways but they didn’t work!

3 Answers 3

  1. Start cmd.exe
  2. cd C:\Pyscripts
  3. python filename.py

For basics like print ‘hello’ , you don’t need any library import statements but for slightly more complex things you will.

Which python do you have installed?

You don’t want to save files in c:\program files under windows. That isn’t a good practice. Setting up a dev directory like you did or under your user directory is a much better option.

Have you added python to the path setting? If you start a command prompt (cmd.exe, not IDLE and type python , what do you get? If you haven’t set the python path, see http://docs.python.org/2/using/windows.html or http://www.katsbits.com/tutorials/blender/setting-up-windows-python-path-system-variable.php. Just update the settings to the version of python you are using e.g c:\python27.

Get a text editor instead of using IDLE. textpad, textedit, vim, emacs ,context, geanie, sublime, and many many more. Or get an IDE. Aptana’s studio 3 is easy to use and free (eclipse with plugins already installed) or the free community version of pycharm.

How do I run .class files on windows from command line?

I’m trying to run .class file from command line. It works when I manually move to the directory it’s stored in, but when I try something like this:

it says it can’t find the main class. Is there any solution to this other than making a .jar file (I know that .jar is the best solution, but at this moment isn’t the one I’m looking for)?

4 Answers 4

The Java application launcher (a.k.a java.exe or simply java ) expects supports up to four different ways to specify what to launch (depending on which Java version you use).

Specifying a class name is the most basic way. Note that the class name is different from the file name.

Here we start the class mypackage.Main and use the -cp switch to specify the classpath which is used to find the class (the full path to the class mypackage.Main will be path/to/classFiles/mypackage/Main.class .

Starting a jar file.

This puts the jar itself and anything specified on its Class-Path entry on the class path and starts the class indicated via the Main-Class entry. Note that in this case you can not specify any additional class path entries (they will be silently ignored).

Java 9 introduced modules and with that it introduce a way to launch a specific module in a way similar to how option #2 works (either by starting that modules dedicated main class or by starting a user-specified class within that module):

Java 11 introduces support for Single-File Source Code Programs, which makes it very easy to execute Java programs that fit into a single source file. It even does the compile step for you:

This option can be useful for experimenting with Java for the first time, but quickly reaches its limits as it will not allow you to access classes that are defined in another source file (unless you compile them separately and put them on the classpath, which defeats the ease of use of this method and means you should probably switch back to option #1 in that case).

Читайте также:  Драйвера для asus k61ic windows

This feature was developed as JEP 330 and is still sometimes referred to as such.

For your specific case you’d use option #1 and tell java where to look for that class by using the -classpath option (or its short form -cp ):

Command to run a .bat file

I’m trying to make my Visual Studio build script execute a .bat file that does something important.

Here is what I’m want to do right now:

But it doesn’t work.

I have to do this to make it work:

But this is pretty difficult to add to the Visual Studio script.

How can I do this in one single line?

4 Answers 4

«F:\- Big Packets -\kitterengine\Common\Template.bat» maybe prefaced with call (see call /? ). Or Cd /d «F:\- Big Packets -\kitterengine\Common\» & Template.bat .

CMD Cheat Sheet

CMD.exe

First thing to remember its a way of operating a computer. It’s the way we did it before WIMP (Windows, Icons, Mouse, Popup menus) became common. It owes it roots to CPM, VMS, and Unix. It was used to start programs and copy and delete files. Also you could change the time and date.

For help on starting CMD type cmd /? . You must start it with either the /k or /c switch unless you just want to type in it.

Getting Help

For general help. Type Help in the command prompt. For each command listed type help (eg help dir ) or /? (eg dir /? ).

Some commands have sub commands. For example schtasks /create /? .

The NET command’s help is unusual. Typing net use /? is brief help. Type net help use for full help. The same applies at the root — net /? is also brief help, use net help .

References in Help to new behaviour are describing changes from CMD in OS/2 and Windows NT4 to the current CMD which is in Windows 2000 and later.

WMIC is a multipurpose command. Type wmic /? .

Punctuation

Naming Files

Starting a Program

See start /? and call /? for help on all three ways.

There are two types of Windows programs — console or non console (these are called GUI even if they don’t have one). Console programs attach to the current console or Windows creates a new console. GUI programs have to explicitly create their own windows.

If a full path isn’t given then Windows looks in

The directory from which the application loaded.

The current directory for the parent process.

Windows NT/2000/XP: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.

Windows NT/2000/XP: The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.

The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.

The directories that are listed in the PATH environment variable.

Specify a program name

This is the standard way to start a program.

In a batch file the batch will wait for the program to exit. When typed the command prompt does not wait for graphical programs to exit.

If the program is a batch file control is transferred and the rest of the calling batch file is not executed.

Use Start command

Start starts programs in non standard ways.

Start starts a program and does not wait. Console programs start in a new window. Using the /b switch forces console programs into the same window, which negates the main purpose of Start.

Start uses the Windows graphical shell — same as typing in WinKey + R (Run dialog). Try

Also program names registered under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths can also be typed without specifying a full path.

Also note the first set of quotes, if any, MUST be the window title.

Use Call command

Call is used to start batch files and wait for them to exit and continue the current batch file.

Other Filenames

Typing a non program filename is the same as double clicking the file.

Keys

Ctrl + C exits a program without exiting the console window.

Читайте также:  Audio control program windows

For other editing keys type Doskey /? .

↑ and ↓ recall commands

ESC clears command line

F7 displays command history

ALT + F7 clears command history

F8 searches command history

F9 selects a command by number

ALT + F10 clears macro definitions

Also not listed

Ctrl + ← or → Moves a word at a time

Ctrl + Backspace Deletes the previous word

Home Beginning of line

End End of line

Ctrl + End Deletes to end of line

There are many possibilities to solve this task.

1. RUN the batch file with full path

The easiest solution is running the batch file with full path.

Once end of batch file Template.bat is reached, there is no return to previous script in case of the command line above is within a *.bat or *.cmd file.

The current directory for the batch file Template.bat is the current directory of the current process. In case of Template.bat requires that the directory of this batch file is the current directory, the batch file Template.bat should contain after @echo off as second line the following command line:

Run in a command prompt window cd /? for getting displayed the help of this command explaining parameter /D . change to specified directory also on a different drive.

Run in a command prompt window call /? for getting displayed the help of this command used also in 2., 4. and 5. solution and explaining also %

dp0 . drive and path of argument 0 which is the name of the batch file.

2. CALL the batch file with full path

Another solution is calling the batch file with full path.

The difference to first solution is that after end of batch file Template.bat is reached the batch processing continues in batch script containing this command line.

For the current directory read above.

3. Change directory and RUN batch file with one command line

There are 3 operators for running multiple commands on one command line: & , && and || .
For details see answer on Single line with multiple commands using Windows batch file

I suggest for this task the && operator.

As on first solution there is no return to current script if this is a *.bat or *.cmd file and changing the directory and continuation of batch processing on Template.bat is successful.

4. Change directory and CALL batch file with one command line

This command line changes the directory and on success calls the batch file.

The difference to third solution is the return to current batch script on exiting processing of Template.bat .

5. Change directory and CALL batch file with keeping current environment with one command line

The four solutions above change the current directory and it is unknown what Template.bat does regarding

  1. current directory
  2. environment variables
  3. command extensions state
  4. delayed expansion state

In case of it is important to keep the environment of current *.bat or *.cmd script unmodified by whatever Template.bat changes on environment for itself, it is advisable to use setlocal and endlocal .

Run in a command prompt window setlocal /? and endlocal /? for getting displayed the help of these two commands. And read answer on change directory command cd ..not working in batch file after npm install explaining more detailed what these two commands do.

Now there is only & instead of && used as it is important here that after setlocal is executed the command endlocal is finally also executed.

How do I run a Java program from the command line on Windows?

I’m trying to execute a Java program from the command line in Windows. Here is my code:

I’m not sure how to execute the program — any help? Is this possible on Windows? Why is it different than another environment (I thought JVM was write once, run anywhere)?

12 Answers 12

Let’s say your file is in C:\mywork\

Run Command Prompt

This makes C:\mywork the current directory.

This displays the directory contents. You should see filenamehere.java among the files.

This tells the system where to find JDK programs.

This runs javac.exe, the compiler. You should see nothing but the next system prompt.

Читайте также:  Линукс что лучше установить

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

This runs the Java interpreter. You should then see your program output.

If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!

To complete the answer :

Compile the Java File to a *.class file

  • This will create a TheJavaFile.class file

Execution of the Java File

Creation of an executable *.jar file

You’ve got two options here —

With an external manifest file :

Create the manifest file say — MANIFEST.mf

The MANIFEST file is nothing but an explicit entry of the Main Class

jar -cvfm TheJavaFile.jar MANIFEST.mf TheJavaFile.class

Executable by Entry Point:

To run the Jar File

In case your Java class is in some package. Suppose your Java class named ABC.java is present in com.hello.programs , then you need to run it with the package name.

Compile it in the usual way:

But to run it, you need to give the package name and then your java class name:

Complile a Java file to generate a class:

Execute the generated class:

Assuming the file is called «CopyFile.java», do the following:

The first line compiles the source code into executable byte code. The second line executes it, first adding the current directory to the class path (just in case).

Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.

This was an enhancement with JEP 330: https://openjdk.java.net/jeps/330

For the details of the usage and the limitations, see the manual of your Java implementation such as one provided by Oracle: https://docs.oracle.com/en/java/javase/11/tools/java.html

It is easy. If you have saved your file as A.text first thing you should do is save it as A.java. Now it is a Java file.

Now you need to open cmd and set path to you A.java file before compile it. you can refer this for that.

Then you can compile your file using command

Then run it using

So that is how you compile and run a java program in cmd. You can also go through these material that is Java in depth lessons. Lot of things you need to understand in Java is covered there for beginners.

You can compile any java source using javac in command line ; eg, javac CopyFile.java. To run : java CopyFile. You can also compile all java files using javac *.java as long as they’re in the same directory

If you’re having an issue resulting with «could not find or load main class» you may not have jre in your path. Have a look at this question: Could not find or load main class

On Windows 7 I had to do the following:

quick way

  1. Install JDK http://www.oracle.com/technetwork/java/javase/downloads
  2. in windows, browse into «C:\Program Files\Java\jdk1.8.0_91\bin» (or wherever the latest version of JDK is installed), hold down shift and right click on a blank area within the window and do «open command window here» and this will give you a command line and access to all the BIN tools. «javac» is not by default in the windows system PATH environment variable.
  3. Follow comments above about how to compile the file («javac MyFile.java» then «java MyFile») https://stackoverflow.com/a/33149828/194872

long way

  1. Install JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. After installing, in edits the Windows PATH environment variable and adds the following to the path C:\ProgramData\Oracle\Java\javapath. Within this folder are symbolic links to a handful of java executables but «javac» is NOT one of them so when trying to run «javac» from Windows command line it throws an error.
  3. I edited the path: Control Panel -> System -> Advanced tab -> «Environment Variables. » button -> scroll down to «Path», highlight and edit -> replaced the «C:\ProgramData\Oracle\Java\javapath» with a direct path to the java BIN folder «C:\Program Files\Java\jdk1.8.0_91\bin».

This likely breaks when you upgrade your JDK installation but you have access to all the command line tools now.

Оцените статью