how to leave cmd open after batch file executes - java

I am current'y taking a Java course and because of security reasons it won't let us set environment variables for the Java compiler. What we have to do all the time is open cmd and then put
set path="path_to_java"
This gets really annoying because when we close the command line it loses the path. I was able to create part of the .bat file but when I execute it, then it closes instantly. I know you are able to put PAUSE but then it won't let us insert any Java code.
Is there any way to create a .bat file and fix this problem so when I double click it creates path variable and it lets us compile Java code?
I currently only have this
SET PATH "path_to_compiler"
CLS

There are several things you could do. One way would be to create your batch file somewhere (e.g. in your profile folder) and make it automatically execute whenever you start a command prompt:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "%USERPROFILE%\init.cmd"
However, since you're not allowed to set persistent environment variables, this may be prohibited as well. In that case you could create a shortcut to your batch script on the desktop, then open its properties and change the target to something like this:
%COMSPEC% /k C:\path\to\your.cmd
%COMSPEC% is the CMD executable, and the option /k prevents it from automatically closing after the script finished.
As a side note, you may want to include the current %PATH% with the path to the compiler, otherwise stuff may stop working (e.g. because some command line utilities can't be found anymore):
set PATH=%PATH%;C:\javac\folder

I haven't checked for duplicates, but I'm pretty sure I've seen a question about this recently. Anyhow, you aroused my curiosity as to how to do this, so I experimented, and fortunately the first thing I tried worked.
So, basically what you should have is...
#echo off
::Add your code under here
set path=path_to_compiler
cls
call cmd
One thing I must ask, is it absolutely necessary to make the variable called "path" instead of something else? I'm asking this because path is an important variable that the interpreter uses to do things.
Anyway, this basically just runs your code then opens up cmd.exe within the current window.

Related

How to make an executable File

Everybody probably has used install .exe files. But how to make them and when does it make sense to make one?
For example, I would have had programmed commercial software in Python, c++, etc. with different files a GUI and pictures and all of the other stuff.
When I want to deliver my product to my customer I don't want to give them a folder and say you need to install Python or Java and execute the program via your command line.
How can I create an executable file that installs the required language and sets up local instances and arranges all files into the correct order?
To create an executable file from a python program, there is pyinstaller. I don't know about java at the moment. The command is as follows :
pyinstaller fileName.py
You can add args (and there are 2 really helpful ones) :
pyinstaller --onefile -w fileName.py
--onefile will put everything into one single file (recommended) and -w will prevent the console from opening when running the .exe file. Add it if you're running a GUI or something. If you need to console for input, don't add -w.
If you want to automate a command line in cmd, create a shortcut leading to C:\Windows\system32\cmd.exe and add /k and your command. For example :
C:\Windows\system32\cmd.exe /k ipconfig
Double clicking on the shortcut will now run the cmd and execute ipconfig automatically. If you want more than one command, you can do command_1 && command_2 && ... && command_n
There is a setup program built into Windows.
Type
iexpress
and follow the wizard.
You need to provide code to run when it finishes to actually install the extracted files.;

Command-line options working in windows, but not Linux

I have a .bat file on Windows/shell script on Linux that starts a large Java application from the command line. It configures the classpath, environment variables, etc.
At one point, it uses RMID to configure a bunch of services which will run in their own JVMs. The problem is that it won't allow me to specify multiple JARs for the codebase property on Linux. It's allowing me to do so on Windows just fine, but I think my syntax/styling must be wrong for the .sh script and am hoping a more experienced Linux user could have some tip. On Windows, the working line looks like this:
SET RMID_OPTIONS=%RMID_VM%
-J-DINSTALL_DIR=%CONFIG_PATH%
-C-DINSTALL_DIR=%CONFIG_PATH%
-J-DINSTALL_DIR_LOCAL=%HOME_DIR%
-C-DINSTALL_DIR_LOCAL=%HOME_DIR%
-J-Djava.security.policy=%PL_HOME%\windows\system.policy
-C-Djava.rmi.server.codebase=
"file:/%HOME_DIR%\jar1.jar file:/%HOME_DIR%\jar2.jar"
-J-Djava.rmi.server.codebase=
"file:/%HOME_DIR%\jar1.jar file:/%HOME_DIR%\jar2.jar"
// more stuff here
The only important lines are the ones setting the rmi.server.codebase property. The above works 100% fine, however, when trying to set multiple JARs in the codebase in Linux, it causes a general failure and the whole RMID command is not executed. My shell script looks like the following:
export RMID_OPTIONS="${RMID_VM}
-J-DINSTALL_DIR=${CONFIG_PATH}
-C-DINSTALL_DIR=${CONFIG_PATH}
-J-DINSTALL_DIR_LOCAL=${HOME_DIR}
-C-DINSTALL_DIR_LOCAL=${HOME_DIR}
-J-Djava.security.policy=${PL_HOME}/linux/system.policy
-C-Djava.rmi.server.codebase=
""file:/${HOME_DIR}/jar1.jar file:/${PL_HOME_LOCAL}/jar2.jar""
-J-Djava.rmi.server.codebase=
""file:/${HOME_DIR}/jar1.jar file:/${PL_HOME_LOCAL}/jar2.jar""
// more stuff here
"
The shell script itself works perfectly fine if only one JAR is specified, but any more and I get a general failure. Any suggestions on what I'm doing wrong? I'm open to try new things to fix this as all my attempts so far have been fruitless.
Under Linux, escaping quotes is done differently. You are attempting to use the Windows specific syntax, which will result in the jar files being passed as separate arguments, instead of a single one, as it should be.
Instead of "" to produce a quote inside quotes, you have to use \" in Linux:
export RMID_OPTIONS="... -C-Djava.rmi.server.codebase=\"file:/${HOME_DIR}/jar1.jar file:/${PL_HOME_LOCAL}/jar2.jar\" ..."
Aside from that, I'm not sure that the file:/ syntax is correct. It's probably either file:// or the absolute file path without anything preceding it, but you'll have to try it out.
You're doing this wrong. You don't need to start rmid with arguments and system properties at all. All that stuff should be specified when you register the ActivationGroup(s) you're going to use, in your activation setup program. That in turn means that all command-line problems should just disappear.

Is there anyway to execute a jar file with a single word (or phrase) in Terminal?

I'm building a server in Java. I want to setup a single word option that will start and stop the server. "start" to start the server. "stop" to stop the server. I tried to use an executable file that launched the jar, but even using that strategy, I still have to type "./start". The punctuation is a little ugly.
I wanted something similar to "git". If you type "git" in Terminal, you are immediately using git's tools. I'm guessing this is because of a symlink? If so, how did the symlink for "git" get setup? I didn't have to manually set it up from what I remember.
If you would like to avoid sh or ./ while executing you may use alias, add following at the end in ~/.bashrc file:
alias sayhello="./sayhello.sh"
And then run the .bashrc file using following command:
source .bashrc
Finally, you should be able to execute your command using just:
sayhello
What OS are you working on? In windows you just have to add the location of start.exe to your PATH, then typing "start" in the command prompt will launch the executable and the jar

How to Run a Simple Java Program

I dad left Java since so long as a result now it happens that sometimes I forget the simple things and used to behave like a Stupid.
To run a Simple Java program say "Hello World" written in Notepad what do I have to Do?
I know the commands javac "Filename.java" and java "Filename" respectively to run it from the Command prompt.
But When I try to do that I got this message:
"javac is not recognized as an internal or external command, operable program or batch file."
and I could not Complie the file.
I hava little idea that we need to do some stuffs like setting the classpath or perhaps the path evnironment variables but it was exactly that I don't remember.
Can anybody please help me?
Thanks,
david
Add a JAVA_HOME env variable to point to the jdk installation directory
To your PATH env variable, add %JAVA_HOME%\bin
Add a CLASSPATH env variable to point to %JAVA_HOME%\lib.
remember to open a new console window and try running javac and java - everything should be fine now.
1) create JAVA_HOME environmental variable set value to java home directory
e.g. c:\program files\java\jdk1.5;
1) set PATH in environmental variable to your java bin directory
e.g. %JAVA_HOME%\bin
and to check classpath is set correctly run javac command on cmd
and this link will help to create and run simple java application
java tutorial
this might be usefull budddy
http://www.apl.jhu.edu/~hall/java/beginner/settingup.html
You need the JDK to be able to run javac.
I suggest you first start coding in eclipse, it provides all the environment set up for you. Once you get good with coding, you can try command prompt compiling and running. That way, you will be confident with language first and then go into the nitty-gritties of the environment and set up.
Its better to use any java IDE either eclipse or netBeans Download Link
But in case if you like to go through Command prompt method, then u need to set the paths. (These are the variables for your OS, that used to know where your commands e.g. java or javac etc are located). Hope from other answers you set the paths.
Good luck

How can I set the PATH variable for javac so I can manually compile my .java works?

Here's the address on my drive:
C:\Program Files\Java\jdk1.6.0_18\bin
How would I go about setting the path variable so I can go in command window (windowskey+r "cmd") and be able to type things like:
javac TestApp.java
I'm using Windows 7 Professional.
That would be:
set "PATH=%PATH%;C:\Program Files\Java\jdk1.6.0_18\bin"
You can also append ;C:\Program Files\Java\jdk1.6.0_18\bin to the PATH in the user environment dialog. That would allow you to use javac and other java tools directly form any cmd shell without setting the path first. The user environment dialog used to be somewhere in the system properties in XP, I have no idea where it is in Windows 7.
Typing the SET PATH command into the command shell every time you fire it up could get old for you pretty fast. Three alternatives:
Run javac from a batch (.CMD) file. Then you can just put the SET PATH into that file before your javac execution. Or you could do without the SET PATH if you simply code the explicit path to javac.exe
Set your enhanced, improved PATH in the "environment variables" configuration of your system.
In the long run you'll want to automate your Java compiling with Ant. But that will require yet another extension to PATH first, which brings us back to (1) and (2).
Step 1: Set the PATH variable JAVA_HOME to the path of the JDK present on the system.
Step 2: in the Path variable add the path of the C:\Program Files\Java\jdk(version)\bin
This should solve the problem.
Happy coding!!
Type cmd in program start
Copy and Paste following on dos prompt
set PATH="%PATH%;C:\Program Files\Java\jdk1.6.0_18\bin"
Follow the steps given here
http://www.javaandme.com/
after setting variable, just navigate to your java file directory in your cmd and type
javac "xyx.java"
or if you don't navigate to the directory, then simply specify the full path of java file
javac "/xyz.java"
First thing I wann ans to this imp question: "Why we require PATH To be set?"
Answer : You need to set PATH to compile Java source code, create JAVA CLASS FILES and allow Operating System to load classes at runtime.
Now you will understand why after setting "javac" you can manually compile by just saying
"Class_name.java"
Modify the PATH of Windows Environmental Variable by appending the location till bin directory where all exe file(for eg. java,javac) are present.
Example : ;C:\Program Files\Java\jre7\bin.
only this will work:
path=%set path%;C:\Program Files\Java\jdk1.7.0_04\bin
You don't need to do any complex command-line stuff or edit any system code.
You simply have to open Computer, showing all of your disks and open properties.
From there, go to Advanced System Settings and click Environment Variables.
Scroll down in the lower list box and edit Path.
Do not erase anything already there. Put a ; after it and then type in your path. To test, open command prompt and do "javac", it should list around 20 programs.
You would be finished at that point.
By the way, the command to compile is javac -g not just javac.
Happy coding!
Trying this out on Windows 10, none of the command-line instructions worked.
Right clicking on "Computer" then open Properties etc. as the post by Galen Nare above already explains, leads you to a window where you need to click on "new" and then paste the path (as said: without deleting anything else). Afterwards you can check by typing java -version in the command-line window, which should display your current java version, if everything worked out right.

Categories