It is possible run a jar that I have inside another jar from my class?
I´m trying to run this command from my class
java -cp //file:/D:/Users/nb38tv/workspace/f2e-core/f2e-mock/f2e-test-framework/target/f2e-test-framework-1.8.3-SNAPSHOT.jar!/h2/sakila-h2-master/h2-1.3.161.jar -ifExists -tcp -web -tcpAllowOthers
But java complain since cannot find the jar.
If I remove the ! from the path I receive this error
Unrecognized option: -ifExists
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
It is not possible to execute a jar embedded in a jar like that, but even if it were possible, your Java command line is simply incorrect. That is because the -ifExists (and the other options) are interpreted as commandline options of the java executable.
You will get the same error message if you extract that h2-1.3.161.jar from the f2e-test-framework-1.8.3-SNAPSHOT.jar and tried to execute it with the same command line.
It would either need to use -jar instead of -cp, or you would need to specify the class to run before -ifExists.
Related
I am sure this is a stupid question and it must have been asked by every java programmer before. But I cannot find a related question at all.
This talks about subdirectories but I don't have any subdirectories as they are all in the same directory as the java file and the directory I executed the command line from Executable jar file error
This solution gives me the same error as I am writing below: Java command line with external .jar
Others (I don't have links to) talk about Eclipse and other IDE but I am not using an IDE, just a Linux terminal.
I am trying to import a public jar file from http://www.hummeling.com/IF97. The downloaded jar file has been renamed to if97.jar.
I have a java file called steam.java with these commands inside the file:
'
import com.hummeling.if97.IF97;
IF97 H2O = new IF97(IF97.UnitSystem.ENGINEERING);
System.out.println("test H2O table PSpecificEnthalpy(1): "+H2O.specificEnthalpyPT(1,300));
System.out.println("test H2O table PSpecificEnthalpy(5): "+H2O.specificEnthalpyPT(5,300));
'
But I do not know how to run this file in the command line.
I successfully compiled by typing:
'javac -cp if97.jar ~/test/steam.java'
Now I have a file called steam.class
But when I execute it with:
'java steam -cp if97.jar'
or
'java steam -jar if97.jar'
I get error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/hummeling/if97/IF97
at steam.start(steam.java:364)
at steam.main(steam.java:341)
Caused by: java.lang.ClassNotFoundException: com.hummeling.if97.IF97
I am trying to execute this in Linux Ubuntu 16.04 using Terminal. Both the files (steam.java and if97.jar) are in the same Home directory where I execute the javac & java command on.
I believe (or I'm mistaken) that the problem is that java isn't able to find the jar file. But I don't know why.
Please advise, thank you in advance.
You need to specify the class name after the JVM options, because whatever coming after the class name are considered arguments for the class, not the JVM.
Try this:
'java -cp if97.jar steam'
I have a program that I run from Eclipse successfully.
However, when I want to run it from terminal, I encounter the famous error:
"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver"
on this line:
Class drvClass = Class.forName("oracle.jdbc.driver.OracleDriver");
PS:
I have the following in CLASSPATH:
/oracle/jdbc/lib/ojdbc6.jar
Also note that I compile it successfully (javac Test2.java). Then when I run it (java Test2), I get the following error:
Error: Could not find or load main class Test2
So I run:
java -classpath ~/Desktop/JDBC2/src Test2
It runs, but I get the above "ClassNotFoundException" though.
I found this question tricky: the reason is related to semicolon after jar file address.
At first I changed the directory of MySample.java to another directory (you can don't do that) like C:\
then I removed package address from the source code, at the end I run this command in cmd
java -cp path_to_oracle_driver.jar; MySample
P.S. If you want run it from terminal you have to remove package PackageAddress from the source code and compile it again.
As #yngwietiger mentioned above in the comments, using -classpath parameter when running the .class file, overrides the original CLASSPATH and the predefined ojdbc6.jar file. So we need to mention both when running:
java -classpath ~/Desktop/JDBC2/src:/oracle/jdbc/lib/ojdbc6.jar Test2
Or, as a better solution, we can add the current path to CLASSPATH (note the colon and dot at the end):
export CLASSPATH=$CLASSPATH:.
And, in order to run, we just need to type:
Java Test2
I am working on a project that has many module jar files, one of which conains my main class; I am trying to write a shell script that will setup the class path and then start the application; here is my script.
#!/bin/sh
java -cp "modules/*;lib/*" com.example.Launcher
In this example, com.example.Launcher is the class that contains public static void main(String[] args)...
The issue that I am facing is that when executing my script by ./myscript I am give the output
Error: Could not find or load main class com.example.Launcher
This would be simple, there is something wrong with the classpath right?
But if directly from the command line I execute
java -cp "modules/*;lib/*" com.example.Launcher
the exact same command from the script, in the directory the script resides, everything works just fine.
Any thoughts?
side note
I am running this via CygWin
another side note
This might be an issue with sh in CygWin. I coppied this build to a CentOS machine and tried executing it, changing the ; to a : allowed for execution from the script.
The issue persists on my window machine even with the change.
a third sidenote
It would again appear that the issue is with sh in CygWin, my final solution was this:
launcher.sh will invoke java using a : in the classpath
launcher.bat will invoke java using a ; in the classpath
On Unix-like systems the seperator is a : (not a ;). Change
java -cp "modules/*;lib/*" com.example.Launcher
to
java -cp "modules/*:lib/*" com.example.Launcher
I'm trying to run Java program using cmd.
When I compile that file using
javac helloworld.java
It works perfectly but when I try to run that file using
java helloworld
I get an error:
couldn't find or load main class.
even though my I put my javac path in class path in system variables and javac working correctly.
After searching on how I can fix it I found that I can use
java -cp . helloworld
because it let you to find .class file.
and it works but I know that I can run java program without this -cp so what this for and how I can run my program without it?
-cp specifies the Java classpath for the JRE you are attempting to start. Look for an environment variable CLASSPATH and add '.'.
-cp is used to set the classpath for the jar file, this flag is same as importing a jar file to eclipse and then use it.
If you want to run without this flag, use should set the classpath first beforing running.
export CLASSPATH=path/to/your/jarfile.jar
If you already have some classpath set
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar
If you want to include current directory
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar:.
As other have mentioned, you can set the CLASSPATH. However, a much better approach is to bundle the .class files in a JAR ans user java -jar nameofthejar.jar.
I know there are similar questions posted before and I went through those but still its not working for me.
I am creating a sample JMS test class (chat application) and uses javaee.jar and javax.jms.jar. I can test it through Eclipse IDE and it works fine. But I am trying to run through command prompt so I can run multiple windows.I managed to compile the Chat.java file and it created the Chat.class. But when i try to run it, I get could not find or load main class. These are the commands I used:
From the src/domain folder:
javac -classpath javaee.jar;javax.jms.jar Chat.java---- this created Chat.Class inside domain folder where domain is the package name
The I ran the following command from src folder
java -classpath javaee.jar;javax.jms.jar domain.Chat ---- this gives me the could not find or load main class domain.Chat error message
But when I run without the -classpath parameter(java domain.Chat), it reads the main() and gives me different error since it cant find the jms jar files.
E:\eclipse\Spring\JMSChat\src>java domain.Chat
Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/MessageList
ener
So basically it finds the Chat.class file when I don't pass in the classpath parameter and it cannot find the class when I use the classpath to add the jars. I tried running it from within domain folder as well as from src folder, but no luck. Any clue what I am doing wrong?
Thanks in advance.
Try this
java -classpath javaee.jar;javax.jms.jar;. domain.Chat
By default java uses the current directory in the classpath. When you use the -cp flag, it does not so the path to domain.Chat is not found.
1) You can run multiple instances of the app from Eclipse, and you can check their outputs by cycling through their allocated consoles by clicking on the arrow next to the console icon
2) try running from your source folder java -classpath .;javaee.jar;javax.jms.jar domain.Chat "." means current dir