javac and java commands through Windows batch files - java

I normally compile things through the command line using:
javac -classpath . Test.java
Similarly, I run them through:
java -classpath . Test
I'm now attempting to save myself the trouble of typing these out every time through batch files. I have attempted to do so through another question from here:
Creating a batch file, for simple javac and java command execution
I've also tried my own way:
cmd.exe
#echo off
javac -classpath . Test.java
Still no luck, however. I have checked that my PATH environment variable is correctly pointing to the latest version of jdk and as I've said, I can compile just fine directly through command line. Upon running the batch file, I just get the command prompt with no error; as if there was nothing under cmd.exe. Could anyone lend a helping hand and slap some sense into me?

When you write cmd.exe, that will start a new command prompt. You don't want that.
When you write #echo off, that means nothing will be printed on the screen after that point. That's what it means. That's what it does. That is why it looks like nothing is happening.
Something would be printed to the screen if you had a compilation error, but probably you don't.
If you want the command prompt window to stay around instead of disappearing, I believe there is an option in Windows to configure that, at least there was when I last used Windows, back in the mists of time.

this worked for me. I think it does what you were looking to do.
This is the code I suggest for the .bat file:
cd C:\Users\John\JavaApps\folderThatContains.java //points terminal to folder
javac Main.java //This compiles .java in said folder
cmd /K "java Main" //cmd /K prevents terminal from quitting after "java Main"

Related

how can I run a jar from /usr/local/bin like a Terminal command?

I have made a calculator CLI calculator app in Java that I would like to use. Since I am actually going to use it quite often, I do not like the idea of typing the long path all the time.
I have read an article on StackoverFlow that you can place a .sh command into the /usr/bin folder, but after the El Capitan update, all System folders are locked even for the root user. So, again, I did some more research, and I found that /usr/local/bin folder is specifically made for "home-made" commands for the terminal. I have made a calculator.sh file with the following code:
#!/bin/sh
-jar /Users/mac/Desktop/Данила/my_apps/calculator.jar "$*"
The article said that I should place it in the /usr/bin folder, but because it is locked, I placed it in the /usr/local/bin, thinking that it is practically the same thing, and that it should work. Of course after I placed it in there and tried to run "calculator" command in the terminal, it did not work at all. I figured that it might need a "chmod" command to make it work. So I used chmod +x /path, but afterwards it still would not work. Right now I am stumped, so any help would be welcome.
First, is /usr/local/bin on your path? echo $PATH to find out.
Second, are you sure you pasted the above script properly?
It seems to be missing java on the command line.
I would expect it to look something like:
#!/bin/sh
java -jar /Users/mac/Desktop/Данила/my_apps/calculator.jar "$*"
Also, since you named it calculator.sh, you have to run it using that name.
The .sh extension isn't needed, so you could just call it calculator.
To this I recommend you create an alias.
alias test="java -jar /full/path/to/jar.jar"
And "test" is a command now, if you like get this command allways add it into .bash_profile.

Having trouble running javac from command line

I am trying to compile java files using either the command prompt or Git-Bash (running Windows 7) but I am having no luck. I've researched this and followed the simple steps of checking the version of Java I have by running:
java -version
from the command line and I get "1.8.0_102".
So I went to my PATH variable and appended:
;C:\Program Files\Java\jdk1.8.0_102\bin
I have checked what seems like a thousand times and this is the proper directory, but when I run
javac
from the command line, I still get the error
'javac' is not recognized as an internal or external command, operable program or batch file.
Can anyone huide me in the right direction?
Try to set
JAVA_HOME system variable to C:\Progam Files\Java\jdk1.8.0_102;
and add %JAVA_HOME%\bin; to your CLASSPATH variable
Then restart the command line and try
It's official, dumbest mistake I've made yet. I wrote
Progam Files
not
Program Files

How to use a jar file in conjunction with another in Java from the command prompt

I am doing an assignment for class and I was given a jar file for the Display board and I have to code some basic functions for it in a separate Java file. Now, I need the Display to be called in the program I'm writing, and so I used:
(I'm using a window PC)
javac -cp lab1.jar;.Wrapping.java
However, every time I try this, it gives me this error:
javac: no source files
usage: javac <options><source files>
use -help...
Now, I've changed my directory to the proper folder and the file is in there, and still, I cannot get it to run. What am I doing wrong? I can get other programs to compile and run from the command prompt, it is just this one that I cannot. Any help?
If your command it posted as you ran it literally, without the space between . and Wrapping.java, you need a space there.
Try this command to compile:
set classpath=lab1.jar
javac Wrapping.java
OR
javac -cp lab1.jar Wrapping.java
Try this command to execute:
set classpath=lab1.jar;.
java Wrapping ::Make sure its class name with main()
OR
java -cp lab1.jar;. Wrapping

Java compile and run using notepad++ and nppexec

Please keep in mind that compiling in the windows shell works, so there is little (if not zero) possibility of this issue being a PATH issue.
I have spent a lot of time research how to do this, and all the results I found online say that you can do:
NPP_SAVE
javac $(FILE_NAME)
java $(NAME_PART)
but that does not work for me. In the NPP_EXEC console, I can type java, and I get the normal results as I would from cmd, but any time I type javac, I get the dreaded error code 2 error:
================ READY ================
javac
javac
CreateProcess() failed with error code 2:
The system cannot find the file specified.
================ READY ================
Edit
I must clarify some confusion:
This solution should run in a single script. The goal is to be able to change code, press a hotkey combination (think F5 in Visual Studio) and it builds/compiles and runs.
The actually issue, iirc, was that notepad++ is not recognizing javac for some reason..
Sorry for the confusion...
I have set it very easily by using this Article or you can also see another blog post which is very easy and helpful.
Now come to the point that how we can set the N++ and NppExec so our program run with on a single hand by N++.
Save this script first with the name of Java Compile
NPP_SAVE
cd "$(CURRENT_DIRECTORY)"
"C:\Program Files (x86)\Java\jdk1.7.0\bin\javac" $(FILE_NAME)
here the main thing is your path of the java compiler, as in my case it is in C directory and most probably in the same of yours but still difference between 32bit and 64Bit OS.
Now save this scrip with another name like Compile and Run
cd "$(CURRENT_DIRECTORY)"
"C:\Program Files (x86)\Java\jdk1.7.0\bin\java" -classpath "$(CURRENT_DIRECTORY)" "$(NAME_PART)"
Now add the script to the Macro in N++ to work it from there,
go to Advance Options within NppExec plugin,
A: Check the box at the top that says “Place to the Macros Submenu”
B: select script from “Associated Script” combo box. It will automatically fill in the “Item Name”
C: Now click the “Add/Modify” button.
D: Click OK. This will exit the Advanced Options box and say that NotePad++ needs to be restarted (don’t restart it until other scripts have been added).
We have to click OK because it’s the easiest way of clearing the boxes to add the next script otherwise it’s likely to overwrite the existing menu option.
E: Repeat these steps to add the other scripts and then restart it.
Its done now.
My solution is adapted from the npp_exec help files (Plugins>Npp_Exec>Help/Manuals>Section 4.7.2). This works no problems for me and assumes that your JDK bin path has been added to the Windows system (or user) environment variable "Path".
NPP_SAVE
cd $(CURRENT_DIRECTORY)
javac $(FILE_NAME)
java $(NAME_PART)
I finally, after 5+ hours of googling and trial and error, have a working NPP Exec script that will compile and run a java program without leaving notepad++.
NPP_SAVE
cmd /K (javac "$(FULL_CURRENT_PATH)" && exit) || exit
cmd /K (cd /D "$(CURRENT_DIRECTORY)" && java $(NAME_PART) && exit) || exit
The only thing left would be finding a way to do the above, without having to call and send parameters to cmd, all in notepad++ and nppexec.
As noted in the comment below, if you're using a package, you will need to edit the second line accordingly. If your package name is the same as your file name, the below should work:
cmd /K (cd /D "$(CURRENT_DIRECTORY)" && java -cp .. $(NAME_PART).$(NAME_PART) && exit) || exit
your origin command should work if you set the PATH correctly, the only thing you need to do is select the NppExec-Follow $(CURRENT_DIRECTORY) option, so that the npp can recognize your .java file.
Or you can also change $(FILE_NAME) to $(FULL_CURRENT_PATH) and change java $(NAME_PART) to :
cd $(CURRENT_DIRECTORY)
java "$(NAME_PART)"
This Script on NppExec has worked in my case. Make sure that your path matches the Java version you installed on your machine.
NPP_SAVE
cd $(CURRENT_DIRECTORY)
C:\ProgramFiles\Java\jdk1.8.0_66\bin\javac $(FILE_NAME)
C:\ProgramFiles\Java\jdk1.8.0_66\bin\java $(NAME_PART)

Run Jar related .Java from cmd

I am trying to do "javac Classname.java" from cmd prompt, and this Classname.java requires Jfreechart libraries/jars, and runs fine if compiled from Eclipse (because project package has jars imported).
But I want to run the file from cmd prompt and its not able to show me the output. It comes with errors like: ("package doesn't exist"), how to fix it? I need the class file and also run JNI commands to create header file.
You need to set the classpath.
You can do this in 2 ways. Either use the -classpath or -cp option:
javac -cp jar1.jar;path/to/jar2.jar Classname.java
Or, if you need it to persist, use the CLASSPATH environmental variable:
set CLASSPATH=path1;path2
javac Classname.java
If you have already managed to run your code in Eclipse, then Eclipse can help you.
In the "Debug" view, you should have something like this remaining after you have run your code:
If you right-click the bottom "terminated" text and select "Properties", you will get something like this:
You can copy the command line content and use that to run your app from the command line, or use it to set the classpath as the other answers have advised.
You just need to add the directory paths and/or .jar libraries to your "-classpath" command-line argument.
Depending on how many libraries you've got, you might well wind up with a .sh script (Linux) or .cmd file (windows) that looks something like this:
http://ubuntuforums.org/showthread.php?t=230258
java -cp jts.jar:jcommon-1.0.0.jar:jfreechart-1.0.0.jar:jhall.jar:other.jar:rss.jar -Xmx256M jclient.LoginFrame .
If you're on Windows, you'd use ";" as a separator (instead of *nix ":").
'Hope that helps!

Categories

Resources