Help Request: Getting Batch Java Compiler Finished To Work - java

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.

Related

Compile project and include libraries using batch script

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 :)

Java main class autocomplete

If I'm trying to run a java program and I don't know the exact name of the Main class, is there any way to use tab completion to figure it out?
java -cp stackoverflow.jar org.<tab>
stackoverflow serverfault stackexchange
java -cp stackoverflow.jjar org.stackoverflow.<tab>
Main IntegrationTest QuestionAnswerConsole
Something along those lines.
Basically you are asking how to configure shell autocompetion to support java. It is possible. Take a look on this discussion: How does bash tab completion work?
I have to say that this is a good idea not only for discovering the main class but also to complete other command line options and a class path. I'd be glad to use such script if you develop it. Good luck.
EDIT
At least on my Ubuntu file less /usr/share/bash-completion/completions/java exists and therefore some completion should work. You are always welcome to improve the script.
The only way to get the Tab Completion that you are talking about is to use a shell that is "Java aware" or a shell script that provides this feature for the java command. git has a similar feature, so I don't think it's completely impossible.
Edit:
According to this question on SU, it is possible to create an autocompletion script for the bash shell. Since the question on SU is slightly different than what you are asking, I don't see a lot of specific details that relate to this question. However, it looks like a good place to start.
I don't know any easy way to get the autocompletion of classes names if they are hidden in a jar file.
On the other hand, you can add a Manifest to your jar to make it auto-executable (ie., you just have to run java -cp ... -jar stackoverflow.jar) !
In the jar archive, add a META-INF folder, and inside that folder, create a MANIFEST.MF file that reads :
Main-Class: org.stackoverflow.Main
(or whatever your main class really is).
Here is some documentation : Setting an application's Entrypoint
Other answers suggest you solutions but as a sidenote, you don't need it if you provide maintainer of the jar file provides a manifest file with path META-INF/MANIFEST.MF. Then java automatically extracts main class from the manifest and you can run it this way:
java -jar stackoverflow.jar

Running java from a bat file in such a way that path doesn't need to be specified

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.

Problems compiling file on School's Unix System

COMPLETE EDIT BUT SIMILAR PROBLEM
What's the best software/plugin to enable FTP on Eclipse? I'm using FileZilla, but is there something better/easier?
You are telling javac to compile gamedata.txt and it is reporting an error that it cannot compile this file.
I'd highly suggest using a tool like Ant to script your compilation/packaging/etc so you don't have to worry about typing in arguments on the command line.
First of all, the -J command line argument is not meant to be literally passed as -J<flag>. Taken directly from the javac man page (you can view the exact same thing by typing man javac into the shell):
-Joption
Pass option to the java launcher called by javac. For example,
-J-Xms48m sets the startup memory to 48 megabytes. Although it
does not begin with -X, it is not a `standard option' of javac.
It is a common convention for -J to pass options to the underly-
ing VM executing applications written in Java.
Really, if you want to make this an executable, you can just use the tools that exist in Eclipse to make an executable. Using the command-line javac adds an extra level of complexity that is unnecessary, and that Eclipse is specifically designed to remove.
In eclipse, you can (I think) use File->Export->Java->Executable JAR File to make your project into an executable JAR that any computer with the Java Virtual Machine can run. That way, your project will work on both your computer and the Unix system at your school. You may have to add gameData.txt manually to the JAR or include it separately in the package, not sure how Eclipse does that type of thing though.
You can only compile .java files. If you remove the .txt file from the list of files to compile, it should work fine. If you want to compile all the files in a directory, you can simply use javac *.java
There are some examples in the javac synopsis.
Edit: Updated link to Solaris examples, which are similar to Linux.

Easy command line Java compile

So I have to send a java project to someone outside our company who has no experience with java and they need to compile it. Is there an easy way to do it on the windows command line that does not require writing out lists of the files?
Personally I think javac should be smart enough to handle
javac *
when in the folder just under the root of the package hierarchy. Is there anything in this ballpark?
Edit: The source folder structure is complex and the is no single entry class so some of the ideas mentioned so far won't work. Thanks though! Think 9 levels deep with code on many levels.
From the folder that represents the base of your package hierarchy, assuming your entry point class is called Main, in a package called app,
javac -classpath . app/Main.java
should generate the correct class definitions. The compiler will ferret out the dependencies and compile whatever other classes are needed. The class files will appear in the same directory as their source files.
If, as you say, you have 'more than one entry' class, you will have to at least identify all those top level classes from the dependency hierarchy, which can be listed as further params to javac, specifying the packages as they occur. Like so, assuming you also need to start with other.Entry
javac -classpath . app/Main.java other/Entry.java
Note that you will still have to figure out which of your classes are tops of independent dependency hierarchies, whether you are creating an ant script or doing it this way.
javac BaseProgram.java will compile BaseProgram.java from the current directory, and all classes it references that are available in source code, in the same directory tree.
If BaseProgram references Class1 and Class2, and they are available in Class1.java and Class2.java in the same directory, then they too will get compiled. Likewise if they are in a package, and the package directory is available, they will be compiled.
Provide them with an Ant script that does the build with the correct libraries on the classpath, etc. The script can also do other tasks such as building JARs, etc.
This requires that that person downloads and installs Ant, but that is not hard. (And there is nothing to stop you from providing them with an appropriate Ant distro to install. Or even sending them a distro ZIP file that has a copy of Ant "preinstalled" in the tree.)
Providing an Ant script means that you avoid them falling into Java newbie traps such as forgetting to set the classpath, being in the wrong directory, forgetting to recompile dependent files and so on. Plus, it is more "professional".
You can build a file listing all the classes you want to compile (extracted from the javac man page) -
Example - Two Arg Files
You can create two argument files -- one for the javac options and
the other for the source file-
names: (Notice the following lists have no line-continuation
characters.)
Create a file named options containing:
-d classes
-g
-sourcepath /java/pubs/ws/1.3/src/share/classes
Create a file named classes containing:
MyClass1.java
MyClass2.java
MyClass3.java
You would then run javac with:
% javac #options #classes
Or you can use *.java on the command line e.g.
javac greetings/*.java
(Again from man javac)
Or why don't you just compile the source into a jar that your customer can run using their JRE - especially considering they are not Java savvy?
A simple way would be by using:
javac *.java.

Categories