-2

I am a beginner at batch file coding and have some experience with the command prompt. I was looking up how to run a java program from a batch file and noticed that I needed to make a path so it could compile and run. What is weird is that when i make the path, compile and run the code, it says that the javac isn't an internal or external command. Can someone explain to me if there is a way that I can make a path using a USB? The intention of my code is to copy over to the desktop,then run the program, and finally return the outputted program file. I want to be able to run this on any windows computer. Here is my code. Thanks. I have tried almost everything.

@echo off
cd /d C:\Users\%username%\Desktop 
mkdir HackerMan
echo d | xcopy /d /s %~d0 C:\Users\%username%\Desktop\HackerMan
cd /d C:\Users\%username%\Desktop\HackerMan\ProgramFiles
set path = "C:\Users\%username%\Desktop\HackerMan\ProgramFiles\Java\bin"
javac IPGrabber.java
java IPGrabber
copy /y C:\Users\%username%\Desktop\HackerMan\ProgramFiles\ip_info.txt  %~d0\IPs\ip_info.txt 
cd /d C:\Users\%username%\Desktop
rmdir /s /q HackerMan
rmdir /s /q C:\$Recycle.Bin\HackerMan
Magoo
  • 68,705
  • 7
  • 55
  • 76
  • 1
    Possible duplicate of [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](http://stackoverflow.com/questions/26386697/why-is-no-string-output-with-echo-var-after-using-set-var-text-on-comman) – Mofi Mar 17 '17 at 07:00
  • If you read the help file for the `SET` command you may notice that it never shows the usage syntax of having a space before or after the equals symbol. – Squashman Mar 17 '17 at 13:28
  • One more hint: `cd /D "%USERPROFILE%\Desktop"` works even with user account name containing space characters and user profile directory being stored on a different drive and/or different directory. `%username%` should be used only for referencing the name of the user account, but `%USERPROFILE%` should be used on referencing the user's profile directory or one of its subdirectories. See also Wikipedia article about [Windows Environment Variables](https://en.wikipedia.org/wiki/Environment_variable#Windows). – Mofi Mar 17 '17 at 13:37

1 Answers1

2

set path = "C:\Users\%username%\Desktop\HackerMan\ProgramFiles\Java\bin"

Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN"

The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".

PATH is the sequence of directories searched by windows to find an executable if it isn't found in the current directory. To change path, use "set path=newdirectory;%path%" which appends the current path to the new directory.

Magoo
  • 68,705
  • 7
  • 55
  • 76