execute a java program through batch script - java

I have a java class "Test.java" which contains certain code.
public class Test {
public static void main(String[] args) throws Exception {
testMount();
}
public static void testMount() throws Exception {
System.out.println(System.getProperty("os.name"));
//Windows
String volumeToMount = "\\\\?\\Volume{****-****-******-********}\\";
String mountPoint = "C:\\temp\\";
mountFileSystem("", "", volumeToMount, mountPoint); //This carries out the operation
}
}
I have already compiled the code in Linux Operating System. I want to run the compiled code through a batch script ( .bat file). How do i do that? What is the syntax for that? If i have to add some external jars, where and how do I insert them in the syntax within the .bat file?

here is an example of bat file for executing a java code from the jar with external jars:
#echo off
if "X%JAVA_HOME%" == "X" goto setjavahome
goto setup
:setjavahome
rem #### MODIFY ##########
set JAVA_HOME=c:\program files\javasoft\jre\1.2
rem #######################
:setup
set JNDI_LIB=lib\ldap.jar;lib\jndi.jar;lib\providerutil.jar;lib\ldapbp.jar
set JSSE_LIB=lib\jsse.jar;lib\jnet.jar;lib\jcert.jar
set COMMON=.;%JNDI_LIB%;%JSSE_LIB%
set EXEC=browser.jar lbe.ui.BrowserApp
set CMD="%JAVA_HOME%\bin\java" -cp %COMMON%;%EXEC%
echo %CMD%
%CMD%

.bat is for Windows; try to compile your Java codes in Windows to EXE (with your external libraries, as suggested by galchen), and add your EXE name along with relative / absolute path to the batch file.
For example, the output EXE is named as test.exe, the batch file should contain:
START C:\PATH\TO\YOUR\EXE\test.exe
Advantage of compiling to EXE is mainly for performance.

Related

How to run a java class on ubuntu?

If I compile and run the program. I don't have any issues.
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
If I add the line package ch01.sec01; it complies correctly with javac. However when I try to run it using java I get:
Error: Could not find or load main class HelloWorld
I have tried the following.
export CLASSPATH=/usr/lib/jvm/java-9-openjdk-amd64/bin:/usr/lib/jvm/java-1.9.0-openjdk-amd64/bin
That is why when you use a package in your code, that path must be the actual path of your java file (That means that your code is supposed to be in a directory called sec01 which is inside directory ch01).
With that being set, when running code inside a package, you need to include the path in the command. To do so, after you have compiled your code with javac, navigate to the root of the path (outside ch01 directory) and type
java ch01.sec01.HelloWorld
This should work.

can't run .jar file /java.jar: line 1: public: command not found

I have Ubuntu 16.04.
and downloaded a JDK with a tar.gz file extension and followed This wikihow to install it.
When I try to run a .jar game (like Minecraft) It works successfully, and I have netbeans downloaded that is connected to the same JDK and compiled some programs that i can run in terminal, But When I type :
./Hello_world.jar
Which is :
package main;
public class project {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
I get this output :
./Hello_world.jar: line 1: $'PK\003\004': command not found
./Hello_world.jar: line 2: $'\b.\020oK': command not found
./Hello_world.jar: line 3: syntax error near unexpected token `)'
./Hello_world.jar: line 3:-oK�}����META-INF/MANIFEST.MFM�1
�0��#��uHh Q���X� ��N1�Ҧ$)��7�(�p�ww
�A����|��}�1���ή�n��p<�Рŗ��:CpN~�s�ν�˚�3��%
��)���goPK`
Simple: JAR files aren't executables. You can only invoke binaries/scripts by telling your shell to ./command.
They are archives that contain compiled Java classes.
Thus you use them like:
java -jar somejar.jar
This starts a java virtual machine, and tells it to open the given JAR file. The JVM will then figure the "main" class to run from the meta information that can be backed into the JAR file - to then "run" that main class.
( assuming that the corresponding JAR file has been built in a why that allows running it like this. see here for details on how you enable this "easy way" of running a JAR file )
And just in case: with some scripting magic, you actually can turn a JAR file into a "binary", see here for example.

How to use the HWUT unit test tool with java files

I've been using the HWUT unit test tool with C and python and now I'm trying it with Java. In python I can tell HWUT to run all .py files with python if I specify a "hwut-info.dat" file as
Some title
----------------------
python *.py
But for Java I can't do that, because java expects the file names without extension. The java compiler shall consider the '*.java' files but it does
expect the file stem as input, i.e. java FileStem in order to treat FileStem.java.
Later HWUT versions (> 0.28.0) will provide some sophisticated functionality to
pass information about path names, i.e. in those (at the time of this writing)
'future versions' the usage of an extension-less java file can be specified as
java -cp mypackages:. {{file.java%}}
For the time being, you need a script to run your test, such as 'runjava.sh':
#! /usr/bin/env bash
java -cp ../../package:. $(basename ${1%.*}) ${#:2}
It calls the java interpreter, cuts the extension from the filename and passes
all remaining arguments to the file to be executed. In your 'hwut-info.dat'
file you need to specify:
My title
--------------------------------------------------------
bash ./runjava.sh *.class
This lets HWUT search for *.class files in the current directory. The
'runjava.sh', though, omits the extension and calls 'java' with the file stem.
Inside your Java test application you reflect on the command line like:
public class Mine {
public static void main(String[] args) {
if( args.length >= 1 && args[0].equals("--hwut-info") ) {
System.out.println("Title ...");
...
}
}
...
}

Getting java.lang.NoClassDefFoundError when trying the execute a java.class

I created the following class located in the MainJPrint.java file
import com.XXXXX.pdfPrint.PDFPrint;
public class MainJPrint
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
print(".....");
}
public static String print (final String url)
{
Object rc = AccessController.doPrivileged(new java.security.PrivilegedAction()
{
public Object run()
{
...
}
}
}
}
In the same folder I have a jar archive jPrint.jar
I compile the class using the following command
>javac -classpath jPrint.jar MainJPrint.java
When I'm trying to execute resulted class file, I get this error:
>java MainJPrint
java.lang.NoClassDefFoundError: com/XXXXX/pdfPrint/PDFPrint
If I uncomment the Hello World line and comment the next line, the program runs fine.
I'm using j2sdk1.4.2 installed at C:\j2sdk1.4.2.
I do also have installed other java versions (at C:\Program Files\Java: jre 1.6.0_01, jre 1.6.0_02, j2re1.4.2, jre6, jre7, jdk1.7.0_03)
The PATH variable contains the C:\j2sdk1.4.2\bin path, however I think the java.exe is loaded from the upper version, but it shouldn't matter and I can call it like
>C:\j2sdk1.4.2\bin\java.exe MainJPrint
jPrint.jar is a third party archive and I need to create an applet which exposes a method so I can call it with javascript. I'm not a java developer, I'm having some little troubles and I'm really on an end here.
I tried other options like:
>java MainJPrint -cp .
>java MainJPrint -cp jPrint.jar
So how can I execute that class file which uses a class located in a separate archive?
To execute a class that depends on external JARs, you need to specify all elements of the classpath on the command line.
If you don't specify a classpath, Java automatically uses . (the current directory), which is why, if MainJPrint didn't depend on jPrint.jar, your invocation java MainJPrint would have worked.
But when you specify -cp jPrint.jar, Java does NOT automatically add the current directory to the classpath, which means that it then cannot find MainJPrint. You need to specify both. On Mac/*nix, the following invocation should work:
java -cp jPrint.jar:. MainJPrint
Or on Windows:
java -cp jPrint.jar;. MainJPrint

Running java code on VMWare with references to external jar files

I am trying to run simple java code on VMWare Workstation. I have the following simple test Main file:
import cern.jet.random.engine.RandomSeedGenerator;;
public class TestDataService {
//private static Logger logger = Logger.getLogger(TestDataService.class);
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World DAI!");
// Input some data.
RandomSeedGenerator re = new RandomSeedGenerator();
return;
}
}
RandomSeedGenerator is a class in colt.jar library, and I have the jar file under my lib folder.
I am building the project with ant, and I have the following manifest file where I set the classpath:
Manifest-Version: 1.0
Main-Class: edu.umass.TestDataService
Name: edu/umass/TestDataService/Version.class
Class-Path: lib/colt.jar
When I run the code from the VMWare shell which runs Red Hat Linux, I get this Exception:
[root#localhost] java -jar app.jar
Hello World DAI!
Exception in thread "main" java.lang.NoClassDefFoundError: cern/jet/random/engine/RandomSeedGenerator
at edu.umass.TestDataService.main (Unknown Source)
Caused by: java.long.ClassNotFoundException: cern.jet.random.engine.RandomSeedGenerator
Just as a final note, everything seems to work fine on windows with eclipse, but nothing seems to work on the virtual machine. Any ideas?
Did you install the jar files required by your application on the VMs?
Did you configured CLASS_PATH correctly?
I doubt there is an issue with the jvm or the vm. The problem is going to be in how you run the class. Specifically how your setting the classpath. Try this:
Navigate to where you've placed colt.jar. Get the present working directory by typing in pwd. Use this to construct the run command using the absolute path to colt.jar.
So eventually you should be running (from the directory containing your jar) something like this:
java -cp /the/full/path/to/lib/colt.jar -jar app.jar
Once you've got that work you can then try and figure out what the correct relative path is. and then you'll be able to do
java -cp a/relativel/path/to/lib/colt.jar -jar app.jar

Categories

Resources