Batch file to setup jdk from memory stick - java

I'm doing a year in computer science at my university and it turns out there are very few computers that have the jdk installed on them. I can run eclipse portable from my memory stick, so compiling java should be possible.
What I was wanting was a batch file that set up the environment variables on the system so that I am able to run the portable version of the jdk from my memory stick. I don't appear to be able to alter the system variables but can change the user ones.
So far I have the below code as a batch file, though it still leaves me with a javac not recognize message when run. As a final note I should point out I don't have a great deal of experience with batch files.
echo %path% > stored_path.txt
setx PATH "%cd%PortableApps\CommonFiles\OpenJDK\bin;%PATH%"
setx JAVA_HOME "%cd%PortableApps\CommonFiles\OpenJDK"
setx CLASSPATH "%cd%PortableApps\CommonFiles\OpenJDK\bin;%CLASSPATH%"
Thanks for any help you can give me.
Update 04/10/13 - 16:54:
Just to clarify Eclipse itself works fine and is fully usable. However I have a jdk saved on the memory stick that works as if I call javac from a console that is open to that directory it works fine. What I want to know is if there is a way for windows to recognise this command from any directory on the system by just changing the user environment variables.
A thought that just occurred to me (I can't test it right now), is that would making a shortcut to the javac exe file in another folder work?

No need to actually set the environment variables permanently!
Just set them for the current session of the program. This is the batch script I made to launch my eclipse setup.
#echo off
setlocal
pushd "%~dp0"
if not exist eclipse.exe exit /b 1
if not exist jdk1.6.0_45\ exit /b 2
if not exist apache-maven-2.2.1\ exit /b 3
set "JAVA_HOME=%~dp0\jdk1.6.0_45"
set "MAVEN=%~dp0\apache-maven-2.2.1"
set "M2_HOME=%MAVEN%"
set "MAVEN_OPTS=-Xms512m -Xmx1024m"
set "CLASSPATH=.;%JAVA_HOME%\jre\lib\ext;%CLASSPATH%"
set "PATH=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%MAVEN%\bin;%PATH%"
rem M2_REPO
mklink /d jre "jdk1.6.0_45\jre"
start eclipse.exe -vm "%JAVA_HOME%\bin\javaw.exe" -data ".\workspace"
rem cmd /k
popd
endlocal
exit /b 0
This sets all that I need and starts eclipse with these settings without having to modify the variables.
My folder structure:
eclipse\
jdk1.6.0_45\
apache-maven-2.2.1\
workspace\
eclipse.exe
all other eclipse folders and files.

Our school-computers didn't have git installed, so i put the portable version of msysgit on my USB-Stick. That gives you (aside from git) a portable MSYS, which is a bourne shell with most of the standard linux tools (like ls and df).
So, i used the shell, instead of the windows Command Line. Then, for the shell, I created a small bash-script to set up the environment-path's. From the gist:
To make the script ("env_setup.sh") execute every time the git-shell
is started, you'll want to put the script into the "etc/"-directory in
your msysgit-folder.
After that, you'll need to edit the "profile"-file, which is executed
by the shell every time it's launched.
Add the following line at
the end of the file:
# Setup the environment-variables
. /etc/env_setup.sh
This is the script, which sets up the environment variables:
#!/bin/bash
# Resources:
# http://stackoverflow.com/questions/623518
# http://stackoverflow.com/questions/59895
# http://markashleybell.com/articles/portable-git-windows-setting-home-environment-variable
# Get the directory this script is in:
DIR=$(cd $(dirname "$0"); pwd)
# Get only the Flash-Drive letter (e.g. "F")
FLASH=${DIR:1:1}
echo "We determined your USB-drive to be on $FLASH:\\"
# Set the home-path to the Flash-drive to get portable SSH-keys:
# --- You'll want to change this to your prefered Home-directory!
export HOME=/$FLASH/PortableApps/git/home/luke
echo "I set your Home-directory to '$HOME'";
### --- EXAMPLES ---
# Set the Java-Home variable to a JDK on USB-Stick:
export JAVA_HOME=/$FLASH/JDK1.6
# Add the Java-Tools to the JDK-folder:
export PATH=$PATH:/$FLASH/JDK1.6/bin
# Add a Maven from your USB-drive to the PATH:
export PATH=$PATH:/$FLASH/PortableApps/apache-maven/bin
# Add Node.js from the local pc to your PATH:
export PATH=$PATH:/c/Programms/nodejs
Here, you can see a few examples, including a JDK I had on the same USB-Stick. It is added to the PATH, so you can write javac everywhere you like and always use the JDK specified.
Note that this is only for one session and that it only works in the MSYS shell!

Related

How can I setup a custom run setup for path(for code runner extension)?

So when I run code runner (using java) it asks for javac
so I have to set up a path, but on the computer that I'm using doesn't allow permanent path so, I've been using:
set path=C:\Program Files\Java\jdk1.8.0_241\bin for cmd
or
$env:Path = "C:\Program Files\Java\jdk1.8.0_241\bin"; for powershell
I'm using the VS Code run in terminal so it gives me the .class (yes I know that if I run it in F5 it has no problems but I don't like the text mess on the terminal XD)
So the question is how can I make a custom command so it sets up the path (be it at startup or every time)
Or, Do I have to modify the default settings? (if so where is the file location)
To set the default path on Windows, go to Control Panel, System, Advanced System Settings, Environment Variables.
For cmd:
1.open a cmd and run the commands:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun ^ /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f
2.create a file in your USERPROFILE(likes:C:\Users\UserName) folder named "init.cmd" and then you can set the commands which you want to run automatically when the Command Prompt is launched.

How to upgrade JDK from Oracle to Zulu in redhat linux

I want to upgrade my Linux JDK from Oracle to Azul-Zulu.can someone helps me to do this.
What to do after downloading the tar.gz Zulu package?
I see that after extracting the downloaded package we need to put it in the path where we need to access java. Is that correct or what we can do more like setting environment variables etc.
the answer to your question depends on what you actually want to start with the JDK. Sometimes is is sufficient to change the path to java binary in a start script, e.g. Eclipse or Tomcat, sometimes you also need to setup the variable JAVA_HOME.
To setup the java for yourself only:
Check your shell by running echo $SHELL
-> /bin/bash
-> /bin/zsh
-> /bin/tcsh
or something like that Locate your shell's config file:
bash: ~/.bashrc
zsh: ~/.zshrc
tcsh: ~/.cshrc or ~/.tcshrc
and optionally most of the shells: ~/.profile or ~/.login check those file(s) if you already have variable declarations for: JAVA_HOME JRE_HOME (and rarely seen) SDK_HOME JDK_HOME
check whether any of those files contain a setup for the variable PATH (probably has)
If you found any of the *_HOME variables from above you can change them to reflect your new java home directory: e.g. zulu11.37.17-ca-jdk11.0.6-linux_x64 located under /opt/zulu11.37.17-ca-jdk11.0.6-linux_x64 would mean to set the variables as follows: zsh and bash:
export JAVA_HOME=/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
export JRE_HOME=/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
tcsh
setenv JAVA_HOME /opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
setenv JRE_HOME /opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
SDK_HOME and JDK_HOME may point to the above locations as well but are normally unused.
If you located your PATH somewhere it may read something like this: zsh and bash:
export PATH=~/bin:$PATH
tcsh:
setenv PATH ~/bin:$PATH
In order for your new java to appear "before" the "other" java you need to update (or create if missing) this to e.g.: zsh and bash:
export PATH=~/bin:/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64/bin:$PATH
tcsh:
setenv PATH ~/bin:/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64/bin:$PATH
After completely logging out and in again your JAVA_HOME and PATH can be controlled in a shell like this:
echo $JAVA_HOME -> should point to *your* java folder
echo $PATH -> should contain *your* java folder's bin directroy
which java -> should list the new java binary instead of /bin/java or /usr/bin/java
BTW: the variables JAVA_HOME, JRE_HOME and PATH may also be set in a startscript, e.g. catalina.sh for tomcat or the eclipse.ini for eclipse if you do not want to modify your environment.
If you download the RPM/DEB instead of the tar.gz and install the package you (may) not need configure anything since the packages might re-configure your system for you
Cheers, Holger

Not able to set JAVA_HOME variable in ubuntu 14.04 on manual installation

I've downloaded java jdk1.8.0.7.tar.gz file from the official website and unzipped it into my home directory. Now to set the $JAVA_HOME variable I used the follwing commands command nano .bashrc and then appending export $JAVA_HOME=/home/shivam/Java/jdk1.8.0.7 at the end of the file . But whenever I run the command sudo $CATALINA_HOME/bin/startup.sh I get an error message saying
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
I don't understand why it is unable to find the java path though I've installed tomcat and set its home variable the this way, which worked.
I wan't to install Java manually without using apt-get. Kindly guide .
You want to append the following to .bashrc:
JAVA_HOME=/home/shivam/Java/jdk1.8.0.7
Note the missing $ at the start.
Also remember that the file won't effect your current shell without sourceing it first.
Also, when running a command with sudo, you are running it as the root user, not as yourself. So the environment variable needs to be set for the root user, not yourself.
You can run sudo env | grep JAVA_HOME to see whether it is set for root.
Have you run .bashrc ?
source .bashrc
Try add the following 2 lines in your .bashrc file:
JAVA_HOME=/home/shivam/Java/jdk1.8.0.7/
export JAVA_HOME

Unable to start Java applications on xfce4 desktop? - Screw you lightdm

I used to use Debian's alternatives system to set 'global env' like java, javac, javap but I've read about the disadvantages.
So I added
export JAVA_HOME=/opt/jdk/java
export PATH=$JAVA_HOME/bin:$PATH
to my ~/.bashrc and when I open my terminal I can use the commands as expected but my most of my shell scripts doesn't work anymore.
As you can see in the picture below they check if $JAVA_HOME exists and executes the following command which does nothing. When I enter $JAVA_HOME/bin/java -version it works correctly. If I start the script in a terminal it works, too.
So it seems that #!/bin/sh doesn't source .bashrc? Changing it to #!/bin/bash doesn't solve the problem.
I tried to add the export commands to /etc/profile but this doesn't seem to get sourced at startup/login.
Does anyone have an idea or keywords? I think the solution is quite simple but at the moment I'm stuck.
Thank you in advance!
UPDATE:
Starting the script in the bash terminal with ./something.sh works fine. Right click and execute or 'Open with bash' (XFCE4 context menu) does nothing.
Bash loads .bashrc only when the shell is interactive and non-login. In your case the shell is not interactive, so .bashrc is not loaded.
.bashrc contains a check that prevents it from executing if the shell is not interactive. Usually the first thing .bashrc does is:
case $- in
*i*) ;;
*) return;;
esac
This will prevent you from calling source .bashrc from your script.
The script should inherit from your parent shell, so you should be getting all variable that have been exported before you ran the script.
Also, the preferred shebang is #!/usr/bin/env bash which is more portable.
So in your case:
Open a terminal window. That will load .bashrc, but just to make sure, run . .bashrc and then echo $JAVA_HOME to verify that the variable was set correctly.
Then your script will simply be:
#!/usr/bin/env bash
java -jar <whatever>
If you have some other script related variable that you want to set, you can do that by sourcing a "settings" script:
#!/usr/bin/env bash
source ~/settings.sh
echo $SOME_VAR_SET_IN_SETTINGS_SH
Adding additional folders to your PATH variable in XFCE4 with LightDM will not work as expected! Shell scripts that use these additional commands and are started from the graphical environment will fail because the can't find the command. Logging the PATH variable while starting one of these scripts shows that the PATH variable is overwritten with the very default one. Why? Because LightDM hardcodes the PATH variable and overwrites it for the graphical environment. Screw you! Look here!
Source: https://ljwo.wordpress.com/2014/02/02/global-path-in-debian-wheezy-xfce/
Disable it or use another DM.

How to fix the classpath/ environment issue and method error when executing on Unix?

My version of Linux/Unix:
Linux localhost.localdomain 2.6.32-358.18.1.e16.i686 #1 SMP Wed Aug 28 14:27:42 UTC 2013 i686 i686 i386 GNU/Linux
2.6.32-358.18.1.e16.i686
I'm trying to run on Unix using terminal with javac TouchyWindow.java and then java TouchyWindow. I have 2 problems about this program.
Problem #1: When I compile it, I need to export "export CLASSPATH=.:$HOME/objectdraw.jar" for this program every time when I try to run it.
I want unix automatically run objectdraw when I try to compile javac TouchyWindow.java. The teacher said it was possible to do it and he gave me a hint:
(Aside: Unix/Linux allows you define environment variables for configuration. The java
CLASSPATH variable is a useful variable. If you edit $HOME/.bashrc and place the following
definition export CLASSPATH=.:$HOME/objectdraw.jar then any java program you compile/run that uses the objectdraw library will not require and explicit classpath definition when running java or javac. If you have downloaded the Virtual Machine (VM), you can see an example of this in the ~cs11xyz/.bashrc file. The definition only becomes active when you login or create a new terminal.)
Problem #2: If I execute "export CLASSPATH=.:$HOME/objectdraw.jar" when I compile TouchyWindow.java, it did compile successfully, but, when I command java TouchyWindow to see the reult, it did not work and the error is this: "Error: Main method not found in class TouchyWindow, please define the main method as: public static void main(String[] args)"
Someone told me to just change the method to public static void main(String[] args). It could work. But, my teacher's website has the same program example just like the code I showed below without using method "public static void main(String[] args)" and still works perfectly fine. If you want to see the example, here is the link: http://eventfuljava.cs.williams.edu/sampleProgs/ch1/textbook/TouchyWindow/TouchyWindow.html
If you click on the source code, you can see that the program is not using public static void main(String[] args).
Please Help.
How to fix these problems, please?
here is my .bashrc
# pre-existing bashrc file may have been moved to a file named
# "${HOME}/.bashrc.old". Check the commands in that file be
# sure they are still needed.
if [ -r ${HOME}/.bashrc.old ]
then
echo "----------------------------------------------"
echo "About to run commands in your old bashrc file."
echo "Delete the file .bashrc.old, or edit .bashrc if these commands"
echo "should not be run anymore."
echo "----------------------------------------------"
source ${HOME}/.bashrc.old
echo "----------------------------------------------"
echo "Done running commands in your old bashrc file."
echo "----------------------------------------------"
fi
here is my .bash_profile
#AUTION: if you choose to make adjustments to PATH,
# it is usually advisable to *add* to the existing PATH
# rather than resetting PATH completely. By adding, there
# is less chance of inadvertently losing important elements.
# For example: set path = ( $path ${HOME}/bin )
[ -r .acms.debug ] && echo EXITING .bashrc >&2
[ -r .acms.debug ] && echo ENTERED .bash_profile >&2
public=${public:-/public}
# All the usual setup is done by the following line. Any additions
# you make should come after it.
#
[ -r $public/bash_profile.adjunct ] && . $public/bash_profile.adjunct
# When this file was first placed in your home directory, a
# pre-existing bashrc file may have been moved to a file named
# "${HOME}/.bashrc.old". Check the commands in that file be
# sure they are still needed.
# You may add commands to the end of this file as needed.
#
# CAUTION: if you choose to make adjustments to PATH,
# it is usually advisable to *add* to the existing PATH
# rather than resetting PATH completely. By adding, there
# is less chance of inadvertently losing important elements.
# For example: set path = ( $path ${HOME}/bin )
[ -r .acms.debug ] && echo EXITED .bash_profile >&2
import objectdraw.*;
import java.awt.*;
public class TouchyWindow extends WindowController{
public void OnMousePress( Location Point){
new Text("I'm Touched!", 40, 40, canvas);
}
public void OnMouseRelease( Location Point){
canvas.clear();
}
}
Use any text editor to add the export command line you provided to the end of your .bashrc. Or, my preference for environment variables that do not need to be adjusted for nested shell processes, add the line to the end of your .bash_profile or .profile file as appropriate.
Once the change is made, you can make Bash rerun the file to pick up the changes with:
. .bashrc
or:
. .bash_profile
or:
. .profile
...depending on which file you edited. However, reprocessing these files, especially the .bashrc file which is processed every time a new bash command is started, may adjust settings such as shell prompts which may be set to reflect the level of nested interactive shell sessions which have been invoked. To avoid that, simply execute your export command line once in each open terminal window to set up the evironment for your current log in session. The edited file will then take care of the setting the next time you log in.
Note that this might still not work quite right depenting on your GUI setup. It will work if you log in directly to a shell prompt, but that is now rare. If the env command does not list the CLASSPATH environment variable or list it with the correct value the next time you log in and enter env at a shell prompt in a terminal window, then you will have to find out how environment variables can be set for your GUI's window manager. If that is necessary and you come back to StackOverflow for help, you will need to provide detailed information about the version of Unix/Linux you are using as well as the window manager for your GUI.

Categories

Resources