Run .bat file with recent changes in source code - java

I run my java source code through a batch file. The problem is, when I make any change in my source code through any text editor like notepad etc. and save the code, the changes do not reflect back when I run the code through my batch files.
Below is the sample of my batch file that I use to run my code.
cd %cd%
set classpath=%cd%\target\classes;%cd%\lib\*
echo %classpath%
echo %cd%
java org.testng.TestNG %cd%\code.xml
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %cd%\test-output\report.html
pause

Disclaimer: This was started as an edit for paulsm4's answer. But in my opinion this change got too big to be a justified edit.
Changes you make to source files will never take effect until you recompile your code.
You can do this by:
Calling javac (or an equivalent java compiler) manually or within your batch file.
Usage of javac based on this documentation:
javac [ options ] [ sourcefiles ] [ classes ] [ #argfiles ]
Arguments may be in any order.
options
Command-line options.
sourcefiles
One or more source files to be compiled (such as MyClass.java).
classes
One or more classes to be processed for annotations (such as MyPackage.MyClass).
#argfiles
One or more files that lists options and source files. The -J options are not allowed in these files.
Have an IDE (like eclipse) do it for you
The IDE will build the application on run if the code has changed
Use a build tool like:
Ant
Gradle
Maven
A build tool combined with an IDE is what most developers do. A build tool is also used for dependency management. Which makes it great if more than one person works on a project, or when the project is moved between machines often.

Of COURSE the "changes you make in notepad" won't take effect ... until you RECOMPILE YOUR code.
You need to execute the "javac" compiler (or equivalent) before the next time you run "java".
Which is precisely why C/C++ programmers use "make", and Java programs have used "Ant".
STRONG SUGGESTIONS:
Get an IDE (I prefer Eclipse, but there are other, excellent choices):
https://www.eclipse.org/downloads/
Learn a build tool (Ant is still very useful):
http://www.mkyong.com/tutorials/apache-ant-tutorial/
A quick'n'dirty workaround is to simply add "javac" to your .bat file. But that means you recompile every single time you run the .bat file - whether you need to or not. That works OK when your program consists of one or two source files ... but it doesn't scale when your app grows to 10s or hundreds of source files.
Familiarize yourself with an IDE, and familiarize yourself with build tools. Time well spent :)

Check, if the source code is newer than the compiled code (I have no idea about Java, so you surely have to adapt the extensions - feel free to adapt it in this answer too to help others, who might stumble over this)
for %%F in (program.txt) do set file=%%~fF
for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:\=\\%" get lastmodified /format:list') do set datetimeSource=%%I
for %%F in (program.exe) do set file=%%~fF
for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:\=\\%" get lastmodified /format:list') do set datetimeCompiled=%%I
if "%datetimeSource%" gtr "%datetimeCompiled%" echo insert here your command to compile the code...
(See here for the reasons for that strange syntax)
wmic is quite slow, so this code snippet needs about a second, but it's the most reliable way to get a comparable format.

Related

run java jar file on HP-UX as UNIX as "os.name"

I need to execute a jar file on HP-UX that I am not supposed to modify.
I unpacked it using jd-gui and found out that I am failing cause in java there is a condition to check the os, leading to different directions for win, macos, freebds, openbds, gnu and so on.
I am quite sure everything would work if I would be able to make my unix command line reply freebds or openbds to the java call
System.getProperty("os.name")
once executed from a jar file like:
java -jar myjar.jar
is there a way to achieve this? some kind of compatibility mode or a way to preset that parameter.
You can use the -D switch to specify system properties. In my experiment this (unexpectedly) even worked with pre-defined ones like os.name. Therefore this should work:
java -Dos.name=linux -jar myjar.jar

In Algorithms I in cousera ,which terminal command line does the teacher use?

it's a good doc for people who first use java
but meet about command line i have a few questions
https://lift.cs.princeton.edu/java/windows/
His terminal code like this:
~/Desktop/hello> ls
Barnsley.java COS 126.iml WELCOME.txt logo.png
~/Desktop/hello> javac-introcs Barnsley.java
~/Desktop/hello> java-introcs Barnsley 10000
but in my idea,it uses cmd.exe so ls should be replaced dir i know
but when i type javac-introcs Barnsley.java
it tells me
'java-introcs' is not an internal or external command, nor is it a runnable program
Or a batch file.
enter image description here
The output you observed means that you haven't completed the installation of some class-specific programs. The installer for this is mentioned in Section 0 of the link you provided. The most likely cause is that there were some environmental variables that were not modified correctly to add the java-introcs executable or alias to %PATH%. However, you are probably able to replicate the intent of java-introcs, as described below.
To figure out how to make the given Barnsley file compile and run, you'll need to add the dependency StdDraw.class to the classpath when running the file. To do this, you can use the java and javac option -classpath or -cp. You can read more detailed documentation on how to do this here.

Making a "macro" command to run a program

I have a Main.java file and I want to run the program passing it test.txt
I know in command line I can write javac Main.java
After compiling I can write java Main test.txt and this will accomplish running the file and passing test.txt
If I wanted instead to be able to just write main test.txt and have that trigger my Main.class file to run is that possible and if so how?
(Edit: Based on your comment, let me expand to add a couple more situations)
If your goal is to have someone else run your program who does not have Java installed, and you do not wish to have them install a Java runtime environment before running your app, what you need is a program that converts the .class or .jar files into a native executable for the platform you are using. How to do this has been covered in other questions, eg: Compiling a java program into an executable . Essentially, you use a program like JCG (GNU Compiler for Java) or Excelsior JET (a commercial product) to expand the byte code into full native code with a mini-JRE built in.
If your goal is to save typing, there are a number of strategies. Others have suggested alias commands, which work well on linux.
A slightly more portable option that you could ship with your program would be a shell script. Granted, shell scripts only run on linux or other OS's with shell script interpreters installed.
Here is an example shell script. You paste this into a text editor and save it as main with no extensio. The $1 passes the parameter argument fyi.
#!/bin/sh
java Main $1
presuming you name your shell script just "main" with no extension, you could call main test.txt to execute your program now.
If you are on Windows, you might want to create a windows shortcut, and point the shortcut to "java Main test.text", using the full paths if necessary (if the paths are not already set). Of course, this does not make the parameter easy to change every time you run it, you would have to edit the shortcut.
add an alias
e.g. under a mac edit your .bash_profile with the following line
alias main='java main'
don't forget to open a new console to see your alias working
Depends on your operating system. On Linux with the bash shell, for instance, you can set up an alias to expand your main into java -cp myjar.jar main.
Linux can also be configured to 'understand' Java class flies as a binary format directly see here (linux kernel documentation).
If you're on windows, you'll have to wait for answer from someone with more knowledge about that than I.
Good luck!

creating 100% standalone executable jar that doesn't require the java command

so apparently if you create an executable jar, in order to run it you still need the java command:
java -jar something.jar
but what if I just want it to run without the java command, so just directly from the command line
something.jar
is there a way to export my java app in eclipse in order to accomplish such
On Unix systems you can append the jar file at the end of an executable script.
On Windows you have to create a batch file.
For instance in Unix:
$cat HelloWorld.java
public class HelloWorld {
public static void main( String ... args ) {
System.out.println("Hola mundo!");
}
}
$cat M.mf
Main-Class: HelloWorld
$cat hello
#!/bin/sh
exec java -jar $0 "$#"
$javac HelloWorld.java
$jar -cmf M.mf hello.jar HelloWorld.class
$cat hello.jar >> hello
$chmod +x hello
$./hello
Hola mundo!
In windows you have to create a batch file like:
::hello.cmd
javaw -jar hello.jar
Which has the same effect.
On Windows and OSX you can double click on the jar to run it, I'm pretty sure you may add a trigger on Linux too.
I hope this help
Excelsior JET - http://www.excelsior-usa.com/jet.html - claims to compile to native code and bring its own runtime support, so it does not require an existing JVM. Commercial product.
I have not tried it myself, but they have spent quite a bit of effort over the years to market JET as a great deployment method for precompiled binaries.
Also note that if you have an executable/runnable jar which works fine with "java -jar someting.jar" and you just want to be able to invoke it in a more convenient way, this is the job of the program accepting your command and launching the java command.
For Linux you can frequently add an alias saying that "something" expands to "java -jar something.jar", and some command interpreters allow for saying that all commands ending with jars should be executed specially. The exact details depend on which shell (command line interpreter) you are using.
What you need is a tool called 'Java Executable Wrapper'.You can use it to Pack all your class files to a Single Executable Package.
The One i recomment is launch4j,you can download it from sourceforge launch4j.sourceforge.net
Launch4J can be used to create standalone Executables (.exe) from a jar file for windows Environment.
The thing is, that Java gets interpreted by the JVM, so you'll at least need to ship it with your app.
To be a little more specific about this, Java gets kind of compiled to byte-code so it can be interpreted faster. But the Byte-Code can't run without the JVM. This is the nice side of Java: You don't need to recompile your Apps to run on other platforms like Linux or OS X, the JVM takes care of that (as it is written in native code and is recompiled for those platforms).
There are some compilers out there which can convert your Java code to something native like C which can then be executed without the JVM. But this isn't the idea behind Java and most of those tools suck at what they do.
If you want your App to run without any interpreter, you'll need to use a compiled language like C or C++
Java program runs on a JVM, for the first question I don't think there's a compiler that can do the job well. For the second question since a jar file is not an executable per se, there must be some sort of settings in the target machine, "executing" a jar file without providing the java command is a matter of convenience for the user. On Windows every file extension has a program associated with it, so .doc documents have (usually) Word as the program associated -that setting is set by the office installer, the java runtime also sets the setting for .jar files when you install it, but behind the scenes, java command will be used by the system. So the short answer to the second question is: depends on the target machine.

Runtime exec output path

I am trying to run a perl command with Java runtime exec in linux/ubuntu/gnome. The command generates an pdf file, but it saves it in my home folder. Is there any way that the exec method can set an output path for the commands executed? Thanks in advance.
The exec method just runs the command on the operating system, so you'll want to change the command you're running with exec more than anything in "Java" per se.
There are a few possibilities:
Change the working directory of your java program. The .pdf is being saved in your working directory because this is where the program is being run.
Unfortunately it's not simple to change this value after the program has been launched. It is, however, trivial to change before the program starts; just change the working directory before starting the program.
Move the file to it's desired location after it's been created in your home directory.
Change the command so that it includes the target location. Your perl script may have an option that will enable you to save it's output to a certain location (usually -o or --output). Using this your program would change from:
Runtime.exec("perl someprogram");
to something like:
Runtime.exec("perl someprogram -o /path/to/some.file")
You might be able to use "output redirection", if there is no option to do this.
Try something like what's below as your argument:
Runtime.exec("perl someprogram > /path/to/some.file")
Unfortunately, without knowing more details of your situation I can't provide more concrete advice.
While each approach has benefits and drawbacks, it's probably best to just implement the one that you understand best; if you can't get one to work, try another.
A good, free online resource for learning is Introduction to Linux: A Hands On Guide.
Section 2.2 has details on cd which you can use for 1..
Section 3.3, section 3 teaches about the mv command, which will be useful in 2..
Section 5.1 is about I/O redirection. Knowing about "output redirection" and the > operator, are important for 4..
For 3., you'll have to consult the documentation of the perl program you're using.
You could modify the Perl script to accept an absolute path for the output.
You can trying setting the working directory using exec(java.lang.String[], java.lang.String[], java.io.File) where File is the directory the command is executed from.
If all else fails, you'll can always copy the generated file from the Home directory to your final location.

Categories

Resources