In CMD I compile project with including libraries
java -cp app.jar;libs/*;. com.app.Main
and it works, but I want create BATCH script, which do exactly the same. I create test.bat and put code like below:
#ECHO off
java -cp app.jar;libs/*;. com.app.Main
PAUSE
But when I run the test.bat the CMD was shown and there is information "Error: Could not find or load main class com.app.Main".
BATCH script is located at the same folder as app.jar and libs folder.
What is wrong with this batch script?
Probably the characters on the compile line are significant to the batch interpreter; try putting the classpath in double quotes.
After Java 6, Classpaths could be built by using wildcard characters.
You can create a directory named classpath and put your JARs inside it. Then you can create your .bat file like this:
#ECHO off
java -cp .;classpath/* com.app.Main
pause
You should have a structure like this:
com
`---app
`---Main.java
classpath
`---your-crital-code-1.0.jar
compile.bat
I see that a lot of people are asking this question on StackOverflow, so here is a little tip for you guys.
I usually avoid using the cd command, as it may create some hassle. In windows, you can Shift + Right Click to open a command window in a particular directory.
I always prefer relative paths over absolutes, so that I don't get into hassle of managing long paths.
Here is a little program that deals with this kind of problem. You can always refer to and contribute to it so that we can make good examples for Java beginners :)
Related
Currently I'm building a small personal project in Java which is a simple file server. I've implemented basic internationalization with usage of ResourceBundle. I'm using .properties files to store messages in different languages.
Till now I was using Vscode built-in java compilation process which was copying .properties files into its corresponding directory in the output. However now I'd like to write a build script for that project.
My command looks like this:
javac -d ./bin -cp ./src ./src/**/*.java.
This command however doesn't take .properties files into account. I searched web whether javac has ability to somehow process/include this files into output but found no answer. I know I can use Maven or Ant, but I'd like to make this project without usage of additional tools.
Answering to my own question, but maybe someone will find this useful one day. In short javac always compiles .java files producing .class files. There is no way that it could process some other file (source:
How to set the output files when compiling with javac).
Long story short if you want to include resources, images, text files, anything that is not .java file in your output, and don't want to use build tools, you have to copy it manually or use cp command in your build script.
I am new to java, Now i am learning. So i googled sample java websites. for example i downloaded source code for online reservation system.
In that code,it have install.bat and start.bat file. May i know what is the use of this file?
Can anyone explain me? Thanks in advance.
You can find out what theese files do by taking a look inside. .bat files are written in plain text. Each line contain one or several commands that can be interpreted and executed by cmd.exe. This kind of files aren't only used for java but for lots of purposes. In your case they are used to launch a java program. In contrast to e.g. C++ Java won't give you an .exe file that you can start by doubleclicking on it. It compiles your code into a .jar file. In order to launch this file you must call the Java virtual machine and tell it to start your jar file (there are some more parameters which you don't have to care about while you are just starting with Java).
However, it will take a while till you'll have to deal with BATCH files. By now you should use your IDE (I would recomend Eclipse) which allows you to start your progrgam without taking care of how to do it. You just hit the "Play"-Button and get your result.
They're nothing to do with Java per se.
They're Windows batch scripts, and from the names, it looks as though one of them is there to install the application on your machine, and one is there to start the application up.
Java is cross-platform, which is a strength for the most part, but it does mean that sometimes applications need a little bit of boilerplate code for starting the application on different platforms. So sometimes you will see Java applications that have a Windows batch file or similar for starting on Windows, and a shell script for starting on Linux, and so on.
Batch files with the extension .bat have nothing to do with Java. They are Windows-specific shell scripts, introduced with MS-DOS in 1981.
A set of multiple commands called as a batch file. Say you are been assigned to compile a set of classes, create jar file and execute. This can be done with a simple batch file like this:
activity1.bat
REM set java home path
set JAVA_HOME=C:\Program Files\java\jdk1.7.0
REM set path
set PATH=C:\Program Files\java\jdk1.7.0\bin
REM set class path
SET CLASSPATH=..
REM compile class files
javac -cp %CLASSPATH% %CUR_DIR%*.java
REM create a jar file
jar cfm new.jar Manifest.txt *.*
REM execute
java -cp %CLASSPATH%;new.jar MainClass.java
PAUSE
Note: REM meant for comments.
I am running Windows 7 64-bit.
I would like to make a .bat file that will run my eclipse project's main.class using (preferably) only that .bat file. The project is still undergoing construction so exporting a runnable jar every few minutes isn't an acceptable solution (nor is installing eclipse on all machines). Furthermore I don't really want to install any more plug-ins, I know there is a way to do this with a .bat but haven't been able to figure it out. Here is what the project root folder looks like:
C:/.../ShootEmUp <-inside there we have:
bin/
lib/
natives/
lots of .jar files
res/
src/
ShootEm/
OtherPackages/
ShootEmMain.java
.classpath
.project
RunMe.bat
I have tried more combinations inside the .bat file than I care to admit. Right now RunMe.bat looks like:
#ECHO OFF
javac src.ShootEm.ShootEmMain.java
java -cp lib/*.jar;. src/ShootEm/ShootEmMain;
PAUSE
This currently produces a javac: file not found: src.ShootEm.ShootEmMain.java error
I have also tried putting the .bat inside the folder ShootEmUp/src/ShootEm/ (next to the class with public static void main [ShootEmMain.java]) and editing the RunMe.bat code to:
#ECHO OFF
javac ShootEmMain.java
java -cp ../../lib/*.jar;. ShootEmMain;
PAUSE
This seemed to work a little better as the error I would get then was:
ShootEmMain.java:5: error: package ... does not exist. (the a whole bunch more for all the other jars in the lib folder)
It's useful to note I've already added C:\Program Files\Java\jdk1.7.0_25\bin; to the front of the value of the Path environment variable.
I have also tried classpath instead of cp and a whole crazy list of syntax combinations for each (once I was well past frustrated) to no avail.
Also I realize my .bat example doesn't set the natives as I'm not really sure where that fits into this mess. Thank you in advance!
You should look at using a tool like ant or maven to build your application. This will streamline producing your artifact.
If it's unreasonable to export a working jar across your environment, I boggle at how you could imagine it's more manageable to export your source code around your environment.
I think I have seen this done, but am not sure where. What I want to do is to create a bat file I can package with my class files when sending to a friend to show them progress/ask advice on non programming matters. My friend is not very handy when it comes to code and doesn't like changing computer settings. Just using java myClass as a command line won't work here because although my friend does have java installed, he has not set his windows environment variables so his command prompt knows where to find java.
What kind of line would I need to add to my batch file to make it so it can compensate for problems like this?
Create a manifest file (manifest.txt):
Main-Class: com.mycompany.myapp.MyMainClass
Package your app as a jar:
jar cfm myjarfile.jar manifest.txt *.class
Create a batch file:
start myjarfile.jar
If it is about sharing and running a single java file without jar dependencies. And you are only worried about the java runtime environment setup, then you can use online java code compilers and executors. Here is one:
http://javalaunch.com/JavaLaunch.jsp
You can google for more!
Use an IDE, NetBeans or eclipse and package your files as a Jar file.. that can be executed directly and you do not need to worry about dependencies, other classes or libraries.
Ok, So I have been making this All-in-One Batch File Compiler for Java Files, to compile the Class Files, Manifest files, and the Jar Files. Everything works but one thing... I can't seem to figure out how to call another path in certain cases.. I will explain a case example below the Batch Code:
# echo off
COLOR 0a
Title Leaum's All-In-One Java Compiler
echo Compiling The Class Files...
"Whattt to do here?!" C:\JavaApps
javac -classpath . *.java
Pause
cls
echo Type the CLASS File Name Exactly...
set Man=
set /p Man=Type CLASS File Name: %=%
pause
echo Compiling The Manfiest File...
echo Main-Class: %Man%>>manifest.txt
pause
cls
I posted it in pastebin because the code thing was being weird.
Anyway, Line 6 for example, let's say I want it to call that Folder to Search for certain .Java Files when it needs to. When ever I am compiling a Java file that contains for example "TextIO.putln" Code, I would need it to call the TextIO.java/Class file in order for it to compile correctly, and I just want to store all the standalone/applet files in a separate directory... And have it include that in the Class Compiler, But still create the Class Files in what ever directory the Current project .Java is in.. If any of this makes sense? I can't seem to find a way to make it call the TextIO though :3.
Any help would be great. Thank you!
Don't re-invent this wheel, especially in a platform specific language!
Learn Maven 3 or at least Ant or even Gradle. SCons would even be preferable to Windows specific batch files, there is nothing of value to learn from the approach you are taking.
If I'm understanding your question right, you simply need to add the directory containing your utility classes to the classpath for the compiler, e.g.:
javac -classpath .;C:\JavaApps *.java
But I do agree with Jarrod and others that, in the long run, it is better to learn to use a build tool.