Batch file to execute java program - java

I have multiple .java files inside a folder (e.g temp/code/project)
I want a batch file that will compile and run these java files.
The batch file should create class files inside the same structure where java files are located starting with the classes folder (i.e classes/temp/code/project)
I don't know how to write a batch file. Can any body help me with this? Thanks in advance.

Theoretically you should write the following:
#echo off
::compile classes
javac -cp YOUR_CLASSPATH com/yourcompany/YourClass1.java
javac -cp YOUR_CLASSPATH com/yourcompany/YourClass2.java
javac -cp YOUR_CLASSPATH com/yourcompany/YourClass3.java
javac -cp YOUR_CLASSPATH com/yourcompany/YourClassLauncher.java
:: create jar
jar cvfM Manifest.txt myjar.jar *.class
echo.
echo Hit any key to launch project.
pause
java -jar myjar.jar
pause
If you have one class that depends on all other classes in your project it is enough to run javac with this class only: compiler will compile everything.
# Manifest.txt
Manifest-Version: 1.0
Class-Path: .;MyUtils.jar
Created-By: 1.6.0
Main-Class: com.yourcompany.YourClassLauncher
This is only example and a good exercise. In real life people use special build tools like good old Ant, Maven or newer Graidle or Buildr. I'd recommend you to take one of them.

Something along the lines of
cd C:\temp\code\project
javac -classpath . -d C:\classes\temp\code\project\ *.java
You may not need to include the -classpath tag (I always do so that I don't have to worry about maintaining my CLASSPATH variable. The -d tag specifies a particular directory to place generated class files.

Related

Making manifest file in java by command prompt

I'm trying to make a 'jar file' and run it by first making manifest file.
as I enter jar command in command prompt and using all explanation, this is what I think is sensible:
>md build\classes ----first I made a directory for my class files--
>javac -d build\classes src\*.java ----then compile java files from src folder to classes folder----
>javadoc src\*.java -d doc ----make documentation----
>jar -cfm my.jar m.txt -C build\classes ----I want to make a my.jar file and put the manifest contents from .class in build \classes to m.txt but I don't know what's wrong that I can't make a manifest file as m----
java -jar my.jar
I mentioned above the problem in making manifest file so that I can run .jar file.
what's wrong with my code for making the manifest file?
this is what it print when I run the command for making manifest file:
Usage: jar {ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Simply add a . at the end of your jar.exe command, or whatever you wish to include from the build\classes directory. The -C command allows you to change directory, but you then need to specify what to include from that directory thereafter.
So...
jar -cfm my.jar m.txt -C build\classes .
I'm assuming that your manifest file is already created. If not, then you can echo via the command line and output it to a file. For example...
echo Main-Class: path.to.YourClass > MANIFEST.MF (or m.txt)

How can i remove part of a directory in batch?

Here's the code
SET PATH="C:\Program Files\Java\jdk1.7.0_40\bin"
dir *.java /b /s >> ./sources_list.txt
javac -cp ".;lib/*" #sources_list.txt -d compiled
dir compiled\*.class /b /s >> .\classes_list.txt
jar cfm app.jar MANIFEST.MF #classes_list.txt
del sources_list.txt
del classes_list.txt
PAUSE
so this is for compiling my java code (1st 3 lines) which works, then to copy the compiled classes into my jar using jar
now my problem is on line 4, finding the compiled classes and printing the path to the classes_list.txt, that works however it returns the full C:\somethin\compiled\something.class
i need it to return only the
compiled\subfolders\something.class
how can i edit that to return the path i need?
jar command can take path to classes and create directory. You need to pass only the directory not all classes.
jar cfm app.jar MANIFEST.MF compiled
should work.

specifying log4j in classpath

I believe this is how I can compile and run a file that uses external library. I'm using Windows.
top level directory
|
|-log4-1.2.17.jar
|-MyApp.java
|-com
|-foo
|-Bar.java
Compiling
javac -cp log4j-1.2.17.jar;. com\foo\Bar.java
javac -cp log4j-1.2.17.jar;"com\foo";. MyApp.java
Executing
java -cp log4j-1.2.17.jar;"com\foo";. MyApp
Compiling itself failed.
simple batch script, for compiling all your project
set COMPILED_CLASSES=.\
set TEMP_FILE=temp
dir .\*.java /s /B > %TEMP_FILE%
javac -classpath log4j-1.2.17.jar;%COMPILED_CLASSES% -d %COMPILED_CLASSES% #%TEMP_FILE%
rm %TEMP_FILE%
add it to top level dir and run
EDIT
step by step
javac ./com/foo/Bar.java -classpath log4j-1.2.17.jar
next
javac ./MyApp.java -classpath log4j-1.2.17.jar;./
run
java -classpath log4j-1.2.17.jar;./ MyApp
Include current directory in java classpath
java -cp log4j-1.2.17.jar;. MyApp
Why do you have to include current directory:
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
Y need to include the local directory. If you want to do it in the current directory it would be something like:
javac -cp .;log4j-1.2.17.jar Bar

How can I compile and run a Java class in a different directory?

I'm writing a makefile that compiles a .java file in a different directory, and then I want to run it, without changing directories. I want to do something along the lines of:
$(SQM_JAVA_TOOL_DONE) : $(SQM_JAVA_TOOL)
$(shell cd /home_dir)
javac myjavafile.java
java myjavafile
where the Java file is /home/myjavafile.java, and the makefile isn't running from /home.
How can I do this?
I might be misunderstanding the question, but you can compile with
javac /home/MyJavaFile.java
This will create MyJavaFile.class in /home
You can then run it by including /home on the classpath. e.g.
java -cp /home MyJavaFile
If you want to generate the class file in a different directory then you can use the -d option to javac.
Use the -d command line parameter with javac to tell it what directory you'd like to store the compiled class files in. Then, to run the program, simply include this directory in the classpath:
javac -d some/directory myjavafile.java
java -cp some/directory myjavafile
Just to add to the existing answers, you may want the --source-path flag:
--source-path <path>, -sourcepath <path>
Specify where to find input source files
I believe this effectively sets the package root javac will compile from (i.e. <path> will be stripped from the expected package name of the files). It's still necessary to enumerate the files to compile, and this should still be relative to the current working directory, not the path passed to --source-path.
For example, to compile and run from a project's root where source is stored in src/ and you want it build in bin/:
$ javac --source-path src -d bin src/mypackage/*.java
$ java -cp bin mypackage.Main
This works even from directories elsewhere in the filesystem, e.g.:
$ javac --source-path /some/absolute/path/src -d /some/absolute/path/bin /some/absolute/path/
$ java -cp /some/absolute/path/bin mypackage.Main
I am using VS Code and installed java and code runner extensions. When I created new java project using the extension, it was creating the .class file in src instead of bin. To solve the issue I opened settings.json file from File > Preferences > Settings and searched for "settings" (or "code-runner"). Then I added following lines in that file.
"code-runner.executorMap": {
"java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && java -cp bin $fileNameWithoutExt",
}
If you don`t want to see the command that runs before code file then add these lines instead:
"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,
"code-runner.executorMap": {
"java": "there is && clear added in the execution paramater"
"java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && clear && java -cp bin $fileNameWithoutExt",
}
I hope this finds someone with similar issue.

java class not found despite providing a jar file

I'm puzzled by the process of running java programs, maybe you can help.
I have several .java files in ~/working_dir/org/project/ that have main functions, and I want to package them in a jar to run them. I do:
cd ~/working_dir/org/projectname
javac -classpath $CLASSPATH *.java
cd ~/working_dir/
jar cf myjar.jar org/
And then try to run one of the classes in the jar by doing:
java -cp myjar.jar org.project.SomeClass
and get
Exception in thread "main" java.lang.NoClassDefFoundError: org/project/SomeClass
Could not find the main class: org.project.SomeClass
What do I do wrong? The classes compile without any errors, and jar tf myjar.jar shows that they're indeed there. As far as I know I don't need to create a Manifest file because I provide the class from which I want to run the main function at runtime - or am I wrong here?
Help much appreciated!
If the exploded jar org/project/SomeClass is beneath your current working dir:
/ <- you are here
+---/org
|
+-----/project
|
+--------SomeClass.class
try java -cp . org.project.SomeClass instead
First of all, note that if you simply do
javac org/project/SomeClass.java
the class file will end up right beside the .java file which makes it tricky to include only .class-files in the jar. I suggest you use the -d option to specify destination directory:
javac -d bin org/project/SomeClass.java
Have a look at the following bash-session for details to get it working:
A listing of the source directory:
user#host:/working_dir/src$ ls -R
.:
org
./org:
projectname
./org/projectname:
SomeClass.java
The SomeClass.java file:
user#host:/working_dir/src$ cat org/projectname/SomeClass.java
package org.project;
public class SomeClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Compile it (with target directory ../bin)
user#host:/working_dir/src$ javac org/projectname/SomeClass.java -d ../bin
List the result and make sure you got the directories right:
user#host:/working_dir/src$ cd ../bin/
user#host:/working_dir/bin$ ls -R
.:
org
./org:
project
./org/project:
SomeClass.class
Create the jar file:
user#host:/working_dir/bin$ jar cf myjar.jar org
Make sure you got the directories right and didn't accidentally include the "bin" directory:
user#host:/working_dir/bin$ jar tf myjar.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/project/
org/project/SomeClass.class
Launch the main method:
user#host:/working_dir/bin$ java -cp myjar.jar org.project.SomeClass
Hello World
user#host:/working_dir/bin$

Categories

Resources