How to compile and run Java with single command line on Windows - java

Everytime I want to build and run my program I do:
javac myProgram.java
java myProgram
I want to do something like this:
buildrun = javac (some_argument).java && java (some_argument)
so after I can just
buildrun myProgram
How to achieve this on Windows?

As other's have suggested you can simply create a batch file to build and run your program. Copy this in Notepad and save as .bat.
#echo off
set /p class="Enter Class: "
javac "%class%".java
java "%class%"
As you want, the batch file will ask for a FileName when it runs. In your case, you can set it to 'myProgram' and it will then compile and run the program.
Just make sure your batch file and your java file reside in the same folder for this script to run. You can always tweak the bat file to even accept the full path of the Java file.

To compile and run Java with single command line in cmd use:
java myProgram.java

You can use Makefile this way:
Create a file named Makefile within the same folder where your java files reside.
Add these contents to Makefile
run: compile
java $(class)
compile:
javac $(class).java
From terminal, run this command:
make run class=myProgram
This way, it will compile first then run your java class in a single command

Yet another solution. Create buildrun.cmd file with the following code:
#echo off
javac %1.java
if errorlevel = 0 goto RUN
:ERROR
echo "Build fails!"
goto END
:RUN
java %1
:END
Now you can pass the class name which should be processed. For example: buildrun MyProgram to compile MyProgram.java and run MyProgram.class
In this case execution will performs only if your class was compiled successful.

Related

Failed to run java in remote command line with external jar file

I want to run my Java code in remote server with external jar file. I referred to this tutorial and run IntelliJ IDE to generate a jar file of my whole project. Now I can run the following code in local terminal successfully.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
java -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try
The code will run successfully. However, when I try the same thing in server. The compile process can be finished without any errors or warnings.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
I have checked that the class file new_server_try.class is generated in the directory.
However the second step will return error as Could not find or load main class new_server_try and I am not sure why this happens.
on the second command try giving the full package name .. like shown below
java -cp "/Users/natsuapo/out/artifacts/main_jar/main.jar:lib/*" my.package.MainClass
also with operating system the command differs, check below
Windows
java -cp "Test.jar;lib/*" my.package.MainClass
Unix
java -cp "Test.jar:lib/*" my.package.MainClass

Java Compiler Script [Mac]

I am trying to create to create a script that will automatically change the working directory to my Java code and then compile the code. I am able to change the directly to my path but I cannot figure out how to to call the java compiler on the "filename" while taking the "filename" as an argument directly from the Terminal.
#!/bin/bash
FILE = “$1”
open -a Terminal /Users/Jarvis/Desktop/Codes/Java/CS\ 49J/
javac “$FILE”
Remove the spaces in your FILE assignment. Also, as #cricket_007 mentions, you don't need to open the terminal app, just change directory.
#!/bin/bash
FILE="$1"
cd /Users/Jarvis/Desktop/Codes/Java/CS\ 49J/
javac "$FILE"
Alternatively (I'm in the habit of using curly braces around variables):
#!/bin/bash
FILE="$1"
WORKING_DIR="/Users/Jarvis/Desktop/Codes/Java/CS\ 49J/"
javac "${WORKING_DIR}${FILE}"

How do I run java program with multiple classes from cmd?

At the moment I am looking for another way to run my Java program from command line, other than adding it to a JAR file. My program has the following number of classes:
The name of the program file - MyProgram
Main class - Server1
second class - Client Handler
Package name - Items
3rd class - User1
4th class - User2
The main class and client handler alongside the package will have to run first in order for user 1 & user 2 to run, because they are client classes and are dependent on the main class.
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
If one class is dependent on another class that hasn't been compiled yet, the program won't run. So you should compile all files before trying to run the program dependent on other files.
If your files are packaged, then something like this
javac com.mypackage/.*java
java com.mypackage.MyClass
you must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
Example. Suppose you have the following
Package Named: com.test
Class Name: Hello (Having main)
Java file is located inside "src/com/test/Hello.java"
then, from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
Note that you can add -d to specify output directory of your class files whenever compiling
$ javac -d output_directory -cp . com/test/Hello
In windows the same thing will be working too, I already tried
Check out this from Oracle official site
Once you compile your code, you then run this from the top level:
java -cp . com.myprogram.MyProgram
That order thing you describe doesn't matter. They all get compiled together, and MyProgram will reference Server1, etc.
It may be more then you want to tackle right now but you might want to consider a build system like Maven. To start try out; How do I make my first Maven project?
You can use it to predefine the build order and if you want have it create a jar for you (or not).
Sounds like you will just need to open multiple command prompts and compile and run them in the order you need them to run. Let me know if I misunderstood question.
TO EXECUTE TWO JAVA PROGRAMS WHICH DEPENDS TO EACH OTHER.
(for example:two files Complex.java and Solution.java, where Soultion.java depends upon Complex.java.
So Complex.java should be compiled first and then the class file of Complex must be linked with Solution.java and then Solution.class must be executed for Output.)
REFER THE IMAGE WITH SYNTAX.
STEP 1:
COMPILE Complex.java
compiling Complex.java
syntax-
javac -d [path_where_class_File_build] [path_of_the_file\filename.java]
(Solution.java and Complex.java are Linked. ie-Solution.java calls Complex.java)
STEP 2:
COMPILE Solution.java
compiling Solution.java with linking Complex.class
with linking Complex.class(above created in step 1)
syntax-
javac -d [path_where_class_File_build] -cp [path_of_the_first_class_created] [path_of_the_file\filename.java]]
STEP 3:
EXECUTE THE Solution.class
java -cp [path_of_second_class_created] [class_Name]
(created in Step 3)

Clojure REPL not launching at Windows command prompt

I have placed the clojure-1.4.0.jar path (C:\clojure-1.4.0\clojure-1.4.0.jar) in my CLASSPATH environment variable. Now when I try to launch the REPL from the command line with the following code:
java -cp clojure-1.4.0.jar clojure.main
I get an error:
Error: Could not find or load main class clojure.main
It used to work before I set up emacs. Any ideas.
You can either add clojure jar file to CLASSPATH environment variable:
/some/where % CLASSPATH=/tmp/clojure-1.4.0.jar java clojure.main
or specify it directly in java arguments:
/some/where % java -cp /tmp/clojure-1.4.0.jar clojure.main
Setting CLASSPATH variable and providing -cp argument to java command at the same time is pointless, because -cp argument overrides CLASSPATH completely. This is the cause of your problem: you seem to be invoking java command not from the directory where clojure-1.4.0.jar is located, so -cp clojure-1.4.0.jar switch makes java program try to locate clojure-1.4.0.jar in the current directory and ignore CLASSPATH. Since there is no clojure-1.4.0.jar in the current directory, the command fails.

How to run java application by .bat file [duplicate]

This question already has answers here:
call java class in batch file
(6 answers)
Closed 9 years ago.
I need to run my Java Application through .bat file.
Can anybody help please.
Simply create a .bat file with the following lines in it:
#ECHO OFF
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;path/to/needed/jars/my.jar
%JAVA_HOME%\bin\java -Xms128m -Xmx384m -Xnoclassgc ro.my.class.MyClass
Sure, call the java executable.
Mine is C:\Program Files\Java\jre6\bin\java.exe, so to run it I would do
C:\Program Files\Java\jre6\bin\java.exe -jar myjarfile.jar
If You have jar file then create bat file with:
java -jar NameOfJar.jar
It's the same way you run it from command line. Just put that "command line" into a ".bat" file.
So, if you use java -cp .;foo.jar Bar, put that into a .bat file as
#echo off
java -cp .;foo.jar Bar
#echo off
echo You Are going to creata Java Class
set /p Name=Enter your Class Name?:
echo Your class Name is %Name% & pause
echo To creat a Notepad
pause
notepad %Name%.java
set path=%PATH%;C:\Program Files\Java\jdk1.6.0_14\bin
pause
javac
echo Your java Path succsussfully set.
javac %Name%.java
pause
echo Successfully Compiled
java %Name%
pause
1)open a notpad
2)copy and past this code and save this file as
ex: test.bat
3)Double Click tha batch file.
4)put your java codes into the notepad and save it as
N.B.:- save this java file same folder that your batch file exists.
javac Application.java
java Application
pause
The javac command will compile the java program and the java command will run the program and pause will pause the result until you cross it.
Call the class which has main() method.
java MyClass
Here MyClass will have public static void main() method.
javac (.exe on Windows) binary path must be added into global PATH
env. variable.
javac MyProgram.java
or with java (.exe on Windows)
java MyProgram.jar

Categories