How to add external jar without Eclipse build path option? - java

My java file jdbc11.java was compiled successfully with javac jdbc11.java command in cmd, after that when I tried to to run java jdbc11 I got this exception:
java.lang.ClassNotFoundException: com.mysql.jdbc:Driver
refering to this code in the file
Class.forName("com.mysql.jdbc.Driver");
, when I tried it in eclipse ,I added to "Java Build Path" the external jar : mysql-connector-java-5.1.20-bin.jar and it run successfully .
in case I didn't fix it with eclipse what should I done in the first try with java jdbc11 command in order to make work ?
note: the jar in the same dir with the jdbc11.java

Try adding the mysql-connector jar to the classpath when you execute your command-line code:
java -cp mysql-connector-java-5.1.20-bin.jar;. jdbc11
http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html

You have to add -classpath in the execute command
java -classpath mysql-connector-java-5.1.20-bin.jar jdbc11

If I'm understanding you allright, what you are missing is a -cp option in your java command line, which is what eclispe would do internally if you were to put it in the build path.

Related

How to include a jar file in classpath and run another jar in linux

I am trying to run a jar file on Linux but it requires another jar file to run. To be specific I have added ojdbc8 separately in Eclipse as I am unable to add it in pom.xml. In Eclipse it works fine but in Linux when I try to run the command
java -cp "/home/eim_master/EirRefresh/lib/ojdbc8.jar" -jar EirRefresh-0.0.1-SNAPSHOT.jar
even after including ojdbc8 jar it still fails throwing
Caused by: java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
I need to run the main class and start the spring boot application
The -cp option is for a directory.
Try java -cp "/home/eim_master/EirRefresh/lib/" -jar EirRefresh-0.0.1-SNAPSHOT.jar
I tried this and it worked for me
java -cp "target/EirRefresh-0.0.1-SNAPSHOT.jar:lib/ojdbc8.jar" org.springframework.boot.loader.JarLauncher

Making a ImageJ .jar from command line

I have modified a ImageJ source code and i have compiled from command line, using the javac command, but Im trying to create a .jar file and I need help.
It is the first time that I'm trying to compile and create .jar from command line. When I created the .jar, I did the next command:
jar cf test.jar ImageJ/ij/*
Directory "ImageJ/ij/" contains the compiled code.
It doesn't fail but when I'm trying to execute the .jar It returns the error "Failed to load Main-Class manifest attribute from test.jar". When I execute a .jar I always do "java -jar file.jar" but, in this case, It doesn't work.
If I execute "java -cp test.jar ij.ImageJ" (ij.ImageJ is the Main Class) It works but I need to execute a macro in Batch mode and... It fails.
java -cp test.jar ij.ImageJ -batch ../MacrosIJ/helloWorld.ijm
Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
...
If I create the .jar in Eclipse It works perfectly, Can someone explain what i am doing wrong?.
If someone can help I will be grateful. Thanks!!
The recommended way to build ImageJ from source is using Maven:
git clone https://github.com/imagej/ImageJA.git
mvn
cd target
java -cp ij-1.50.jar ij.ImageJ

Run java program sing Cmd

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.

Add jar files to classpath at launch time

I want to add some jar files to my binary at runtime, but I keep getting errors. I'm running this on a Windows machine. My code is in a directory called SeleniumTest.
Here is the command I used to compile:
javac SeleniumTest\src\com\src\test\First.java -d SeleniumTest\bin -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar
This worked successfully. However when I try to run this command:
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin com.src.test.First
I get a message:
Error: Could not find or load main class SeleniumTest\bin
My code, First.java exists in
SeleniumTest\bin\com\src\test
What am I doing wrong?
try this
java -cp "SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar;SeleniumTest\bin" com.src.test.First
try following
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin SeleniumTest\src\com\src\test\First

Error: Unable to access jarfile Click-The-Block.jar

I have made a simple game that I want to convert into a runnable jar so I can show others and launch it without Eclipse.
In Eclipse I:
Right clicked on Project
Export
Java > Exectuable Jar File
Launch Configuration: CTB (1) - Click The Block
It made a jar with a MANIFEST.MF containing:
Manifest-Version: 1.0
Class-Path: .
Main-Class: uk.co.robertmerriman.ctb.main.CTB
This was all extracted to my desktop in Click-The-Block.jar
When I double click, nothing happens.
When I type "java -jar Click-The-Block.jar" into CMD, I get the following error:
Error: Unable to access jarfile Click-The-Block.jar.
Make your jar file name without spaces locally, then write in cmd like the following:
java -jar YourjarFileNameWithoutSpaces.jar
Looks like an OS issue. If you are running windows 07 or vista try running the same in administrator mode.
Also make sure that when you are running the command
java -jar Click-The-Block.jar
You are running it from the directory where the jar is located.
if your java.exe is in c:\java\bin\java.exe and your Click-The-Block.jar is in c:\dev\
Then try out the following command
>
c:\java\bin\java -jar c:\dev\Click-The-Block.jar
Also try renaming your jar file with no cps letter, i know this will not have any effect but there's no harm in trying.
If you are still not able to execute the jar then try downloading an executable jar file from the net and see if you are able to run it using the standard java command. If you still get the same error which means the problem is not with your jar but somthing to do with how java is configured in the system. In that case you can go ahead and reinstall a fresh jdk and try al over again.
Hope it helps ...
I had the same issue with my executable jar generated by maven-shade-plugin "DCD-Desktop-1.0.jar" , I removed the dashes and renamed it to "dcd.jar" and it worked fine .

Categories

Resources