How to set JAVA_HOME in a bat file before accessing Java - portability

I am making a portable minecraft that works completely off a flash drive, including java. At my home computer the .bat file works perfectly, however on the school computer it does not run and gives me "java is not recognized as an internal or external command, operable program, or batch file" leading me to believe that the batch is looking for java on the local computer as apposed to my intentions of using the java portable installed on the flash drive.
Here is my code.
#echo off
Title Do not close this window
set APPDATA=%CD%\mcp_data
set JAVA_HOME=%CD%\mcp_data\Java\bin
java -Xmx3072M -jar "%CD%\mcp_data\launcher\MineCraft.jar"

You never set the PATH variable to include java anywhere. Your home system knows where to find a copy of java (because it's most likely installed on your machine too) but the school system doesn't.
So you can either try and change the path variables on the school computer (which you may or may not have the rights to do, and it leaves a trail they might not like), or you can make your bat file refer to the copy of Java on the flash drive by it's full path name, i.e. %CD%\mcp_data\Java\bin\java.exe or whatever the path of the java executable is.
I recommend using the full path name since it's in a bat file, so the fact that it's longer doesn't really matter.
See below.
#echo off
Title DO NOT CLOSE THIS WINDOW!!! Closing this window will force close MineCraft
set APPDATA=%CD%/mcp_data
"%CD%\mcp_data\Java\bin\java.exe" -Xmx3072m -Dorg.lwjgl.opengl.Display.allowSoftwareOpenGL=true -jar "%CD%\mcp_data\launcher\MineCraft.jar"

Related

Run .exe anywhere in cmd without PATH variable

This works (Notepad++):
C:\Anywhere> start notepad++ hello.txt
And this works (SoX for removing silence in sounds):
C:\Anywhere> sox in.wav out1.wav silence 1 0.1 1%
Yet, my PATH variable includes neither (would send on request).
How can I do this with my program?
To run from everywhere.
Also, why doesn't Notepad++ work without the start command?
(I did this workaround by putting the .exe in C:\ and then simply calling C:\Anywhere> /myprogram but I'm still curious about the above.)
To get the indicated scenario where
you can start applications from anywhere using the start command (or the windows Run dialog) without including its parent folder in path variable,
but you can not start the application without the start command from any directory whithout including the full path to reach it (it is not in the path) or being located in the adecuated directory
the applications are included in the registry under the key
HKEY_CLASSES_ROOT\Applications
note: It is a "merged" view showing the combined contents of
HKEY_LOCAL_MACHINE\Software\Classes\Applications
HKEY_CURRENT_USER\Software\Classes\Applications
If you are not administrator to change the local machine configuration, you can always modify your user registry information to include the applications you need.
edited There is a second place in registry that will allow to include an application in the registry to be executed using start command, Run dialog or from anything that uses the ShellExecute or ShellExecuteEx API calls.
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\

attempting to assign alias to path of an exe file in dos shell

I want to set an alias to my installation of firefox so I can easily start a web page, the problem is that I dont want the script to be system dependent.
Namely I want it to be able to run on a linux distribution where the command to start firefox is already mapped to 'firefox' and can easily be run that way through bash, but on my windows machine I cant seem to get it to assign to the same variable.
I saw that I could set it to '%firefox%' via the set command but that's not quite what I want.
I believe creating aliases is possible on a windows environment because the version of svn that I use auto-installed and was able to assign itself to 'svn'. Anyone know what was involved in them being able to get their alias working, or a similar way to alias a command?
If you include your Firefox path in the %PATH% environment variable, you can start FF with "firefox". Under Windows, you should edit the system-wide settings (see this link).
AFAIK, there is nothing similar to aliases under DOS/Windows (except the %firefox% way you mentioned, too). The 'svn' command you talked about most likely is the same thing, a 'svn.exe' and its path included to %PATH%.
This is a bit restrictive, as you can only use the original filename to launch a program, but you can work around this by creating a batch file in the program's path that launches the program, f.e. a FF.BAT that contains "firefox %1".
Alternatively, you can place a batch file in a path that already is in %PATH%, f.e. the Windows directory. That way, you don't have to modify %PATH%.

ActivePerl Installation on Windows operating system

I have installed ActivePerl on my Windows OS. I have followed below URL
procedure to install
ActivePerl Installation
After having done that, I have tried to run "perl -v " on the command line. But it reports the following error.
The system cannot execute the
specified program
What do I need to do to solve these issues?
I was facing a similar issue... but the thing was that I could execute the file by right clicking the file and opening it with perl command line interpreter.... but still the perl-v command would give the error... all I had to do was execute this command
set PATH=C:\Perl\bin;%PATH%
This solved the issue...
You need to make sure the directory where the Perl executable lives (it might be C:\perl\bin, but basically wherever you told ActiveState Perl to be installed) is in your PATH environmental variable (you can find the variable value by typing set PATH command on command line prompt in Windows).
If you're not sure where you installed Perl to (and can't find it in the default C:\perl\bin), you can find the directory by going to Start menu, finding ActiveState Perl folder, and right-clicking on "Perl Package Manager" icon, then pick "Properties" from the right-click menu. Properties window (in the "Shortcut" tab) will have a "Target" line showing the directory.
I was getting a similar error after installing ActiveState Perl on Windows 8 x64 bit edition and trying to invoke 'perl' at the command line.
'perl' is not recognized as an internal or external command, operable
program or batch file.
I remember selecting the option during installation to add the Perl directory to the system PATH environment, and after checking the system properties, it was indeed showing in the system PATH.
I tried installing 'Microsoft Visual C++ 2008 x86 and x64 redistributable setup' files as suggested by a few places but it still did not resolve the issue, until I tried some of the suggestions in this thread.
At the command prompt I entered:
set PATH
And surprisingly it did not list the Perl directories as being included in the PATH variables.
So to remedy that I entered this into the command prompt and hit enter:
set PATH=C:\Perl64\bin;C:\Perl64\site\bin;%PATH%
(The directory paths are for the 64 bit edition of Perl, adjust according to your installation) the %PATH% portion is important and ensures your existing settings are kept and not wiped out and overwritten when you set the PATH.
That fixed it and entering 'perl -v' into command prompt successfully replies your Perl version. If you had a PowerShell window open before setting the PATH variable, you will need to close it and re-open another instance of PowerShell.
I believe the original underlying issue was something to do with different PATH variables for 32-bit and 64-bit environments and possibly some internal Windows redirection that takes place automatically.
This doesn't sound like a problem with PATH - I would expect it to give the message 'perl' is not recognized as an internal or external command, operable program or batch file.
I have not seen this error message, but http://nirlevy.blogspot.com/2008/03/system-cannot-execute-specified-program.html makes some suggestion for related programs.
Or maybe ask on an Active State forum.
I had the same error. I was able to solve it by changing the order of the Perl64 entries in the PATH variable in the Environment Variables. I moved the C:\Perl64\bin to be before C:\Perl64\site\bin and it worked.
I had a similar error which was solved by adding the .pl extension to the script name, which I had forgotten to do.
I could not get it to work otherwise even with my Perl's location (C:\Apps\Perl\bin) verified as in %PATH%.
The problem lies in the installation directory.
The Perl PATH variable will be set to C:\Program Files\perl (depends on 32 or 64 bit of course), BUT, the default installation directory is C:\perl. This is kind of sneaky actually as you would assume the installer would be more intelligent about this, but it sets the environment variable to that directory no matter WHERE you install the damned thing.

CMD: Bat to Exe Converter - Temp directory problem

i am using 'Bat to Exe Converter' to convert my batch files to exe format.
Now, i am running into some problems.
Whenever i convert something, and i set 'Working Directory' to 'Current Directory', and i start my exe in echo on mode, this is what i end up with to check if there is a specific file in the directory of my exe:
the actual command: if not exist "%~dp0\file.txt" goto :nofile
output: if not exist "C:\Users\MyUser\AppData\Local\Temp\4CBC\\file.txt" goto :nofile
Can anyone help me with this? I don't want it in the temp directory, i want it to be in the directory of my exe.
Thanks.
Without having Bat to Exe changed by the author, I think you have two options:
Remove the need for accessing %~dp0
Perhaps you can merge file.txt with the include option of Bat to Exe into the EXE file. If so, "file.txt" will automatically be unpacked in the current directory when running your compiled exe, and you can it access by %CD%\file.txt.
Get %~dp0 from outside and pass it to the exe as a command line parameter.
This can be done by a simple starter bat file that resides in the same directory as your compiled main batch file. This script schould contain the line
YourCompiled.Exe %~dp0% %%*
Your compiled exe then gets its directory from %1. So you cannot pack everything into one exe, but the main portion of it, perhaps that is sufficient for you.
Well, apparently your batch to exe converter simply packs the batch file and extracts it to a temporary directory prior to execution. Very simplistic, hard to get wrong (compared to actually understand the batch file) but it introduces errors such as the one you're describing.
Your best bet is probably to use another batch to exe converter; some of them are actually a little more sophisticated.
Generally, this is not a good idea. firstly, its prone to errors and instability of the converter on different cmd features. secondly, a determined hacker can still decode what you are doing with the batch. My suggestion, if you are so afraid of people looking into your batch,
1) let only the people who are authorized to use your batch to use it
2) give them the correct permissions.
OR, don't use batch at all
1) create a central interface such as a web interface, where all tasks to be done goes through that interface, like using an ATM machine where only buttons are allowed and all the available user options can be done by pushing buttons...etc..
2) authenticate your users through a central authentication system, eg Active Directory, or LDAP or a database.
This is an 2.5 yr old subject but there is an answer to this so I'm posting for anyone else that happens to find this in a search.
B2EC written by Fatih Kodak, has an option to "Submit current directory".
When this is used, you can reference %1 in your batch file to get the path of the EXE that was executed (instead of the path of the extracted BAT that is really being run).
Hovering over that option in the UI shows "Submit the current working directory as the last parameter". The "last parameter" in my use has always been %1 but you can test your code to be sure.
The latest version, 2.1.4 at time of writing, of Bat to Exe by Fatih Kodak creates an Environmental Variable at runtime that can be substituted in place of %~dp0 to reference the Exe's path. Therefore, you can simply replace %-dp0 with %b2eprogrampathname% in the original batch file.
You can use external folders with f2ko's batch to exe converter. Having
a separate folder for subroutines can neaten up a project folder.
To call mysubroutine that is located in mysubroutinesfolder\mysubroutine,
...
pushd mysubroutinesfolder
call mysubroutine
popd
...
The call can be made a one liner:
call xqt mysubroutine
where xqt.cmd is a program that does the call for you:
pushd mysubroutinesfolder
call %*
popd
exit /b
(the %* means "all of the arguments").
In this way your batch programs run as batch, and UNMODIFIED they will
compile with the bat to exe converter, creating a completely folder independent executable. Select "temporary directory",
and include all of the subroutines/executables in your mysubroutines folder
by "selecting them all" with your cursor as usual, then hit "copy".
Be sure to include the xqt.cmd program too; place it "outside" of your mysubroutines folder. Make sure that is is accessible by your main program. Remember to select x64 if you
are runnning on a x64 machine, or the executable will not find SYSTEM32
files. You can find f2k0's batch to exe converter at:
http://www.f2ko.de/programs.php?pid=b2e
Try this development environment for batch scripts, Batch Compiler . It has everything you need to develop a batch program.And compile into stable stand alone executable (Exe).
Friendly user interface.
Debugger, Check your code for syntax errors.
Powerful, versatile compiler.
Allows mouse input in batch files.
Use Windows Common Dialog Boxes.(BrowseFiles,BrowseFolders)
Draw graphics in batch files.
Reverse engineering proof encryption of source code.
Include Company name, Copyright info and Version info.
Make invisible(silent) executables.
Executables with administrator privileges.
Run & debug your script while editing.
Embed resources with executable.(music,images,files)
Advance Commands (BrowseFiles,LaunchSilent,MouseCMD)
Stand-alone executables.No dependencies needed.
Executables are woking on almost all windows operating systems.(98 to 10)
Quick download : http://bc.gotek.info/files/BatchCompiler159.zip
Cheers!
%cd% will give you the current directory:
if not exist "%CD%\file.txt" goto :nofile
Use %CD% instead of %~dp0.
EDIT:
B2EC is not a real converter. Creation location of equipped .cmd file was chosen to be %TEMP% and this is a good choice. Application just lacks 3rd option for working directory of the script - .exe file directory. I advise you to mail the author about adding this one.
Different paths for .exe and created .cmd lead to information loss, i.e. we are unable to know .exe directory and current directory at the same time without providing additional information to the script (e.g. using environment variable or passing it as first/last argument to the script). This script would need to handle it and we would end writing cmd scripts tailored for this converter, which is bad.
%~dp0 - script directory (%TEMP%/.../) - practically useless
%cd% - working directory (as set up in the converter) - currently there are only 2 options: current directory (working directory of .exe) and temporary directory (actually equal to %~dp0, but without trailing backslash)
I think it can be solved by patching cmd.exe instance in memory to change the script path, but that's B2EC developer's duty.
Side note: Normal executable files can be easily executed with specified 0th argument by providing appriopriate lpApplicationName and lpCommandLine to CreateProcess function. Command files are executed via cmd.exe, so 0th argument cannot be set this way.

Windows JAVA HOME problems

I'm trying to experiment with OracleHelp for Java on my Windows Vista server. I downloaded Oracle help, and I'm following their installation instructions which states:
Unzip the OHJ installation .zip file into a directory of your choice
Ensure that you have the JAVA_HOME environment variable set to the location of your compatible Java SE installation
In the OHJ installation directory, there is a bin subdirectory containing Windows .cmd files and Unix/Linux shell scripts. On Windows platforms, double click on the .cmd files to launch them (or type the .cmd file name on the command line). On Unix platforms, type "sh scriptName.sh" to execute the shell scripts.
ohguide.cmd (ohguide.sh) - launches the Oracle Help Guide documentation
choiceDemo.cmd (choiceDemo.sh) - launches a demo of Oracle Help features
cshDemo.cmd (cshDemo.sh) - launches a demo of context sensitive help
helpsetDemo.cmd (helpsetDemo.sh) - launches the Helpset Previewer for testing your helpsets
authoringWizard.cmd (authoringWizard.bat) - launches the Helpset Authoring Wizard
When I set JAVA_HOME on windows I can set it with or without quotes. Either way fails :
with quotes:
C:\Users\Amir\Desktop\ohj-5_0_0_433\ohj-5_0_0_433\bin>set JAVA_HOME="C:\Program
Files (x86)\Java\jdk1.6.0_14"
C:\Users\Amir\Desktop\ohj-5_0_0_433\ohj-5_0_0_433\bin>ohguide.cmd
C:\Users\Amir\Desktop\ohj-5_0_0_433\ohj-5_0_0_433>""C:\Program Files (x86)\Java\
jdk1.6.0_14"\bin\java.exe" -classpath "ohj.jar;help-share.jar;oracle_ice.jar;jew
t.jar;share.jar;help-demo.jar" oracle.help.demo.ChoiceDemo "demodoc\ohguide\ohgu
ide.hs"
'""C:\Program' is not recognized as an internal or external command,
operable program or batch file.
without quotes:
C:\Users\Amir\Desktop\ohj-5_0_0_433\ohj-5_0_0_433\bin>set JAVA_HOME=C:\Prog
iles (x86)\Java\jdk1.6.0_14
C:\Users\Amir\Desktop\ohj-5_0_0_433\ohj-5_0_0_433\bin>ohguide.cmd
'Files' is not recognized as an internal or external command,
operable program or batch file.
No Java Virtual Machine found; please set JAVA_HOME environment variable.
The problem is caused by the blanks embedded in your JAVA_HOME. When I install the JDK on Windows, I override the installation location with a directory path that does not contain any blanks. There are still a surprising number of tools that cannot deal with blank spaces.
In your particular case, the problem caused by an inconsistency between _init.cmd and ohguide.cmd. In one place, they have double quotes around a use of OHJ_JAVA_HOME and in the other case, they do not have double quotes.
But there is a solution - use the shortened name for the directory. You can find the name using the DIR /X command in a DOS window. For example, on my system "C:\Program Files" has the short name "C:\Proga~1". You can use this value when setting JAVA_HOME, without any quotes. e.g.
set JAVA_HOME=c:\progra~1\java\jdk1.6.0_14
I set it in Windows System Properties and that works fine.
On Vista:
Click the Start button (windows logo, lower left corner)
Right-Click Computer
Select Properties
Select Advanced system settings (options on the left)
Select Environment Variables (button)
Add (or edit) a System Variable JAVA_HOME
Enter your JAVA_HOME without any quotes
Add to the PATH System Variable to include the path to your JDK (so you dont have to worry about how to quote it.
You may also want to extend your CLASSPATH System Variable to include the ones you would specify on the command line (optional)
In both scenarios you are using one too many quotes when you try to call the java exectuable.
In your code this:
C:\Users\Amir\Desktop\ohj-5_0_0_433\ohj-5_0_0_433>""C:\Program Files (x86)\Java\
jdk1.6.0_14"\bin\java.exe" -classpath "ohj.jar;help-share.jar;oracle_ice.jar;jew
t.jar;share.jar;help-demo.jar" oracle.help.demo.ChoiceDemo "demodoc\ohguide\ohgu
ide.hs"
should be:
C:\Users\Amir\Desktop\ohj-5_0_0_433\ohj-5_0_0_433>"C:\Program Files (x86)\Java\
jdk1.6.0_14\bin\java.exe" -classpath "ohj.jar;help-share.jar;oracle_ice.jar;jew
t.jar;share.jar;help-demo.jar" oracle.help.demo.ChoiceDemo "demodoc\ohguide\ohgu
ide.hs"

Resources