How to run a java program including a .jar file - java

I have 2 classes
CLASS1 (the main class)
- calls threading glass
CLASS2 (the threading class)
- calls function
- calls json.simple.jar functons
and a .JAR (https://code.google.com/p/json-simple/)
jar/json.simple.jar
Now the main problem is that i actualy managed to make both classes to compile with those commands
javac -d . -cp '.:jar/json.simple.jar' *.java;
this will create a folder named package1 and then when i try to run it it gives me the error
java javanolo.CLASS1 -jar 'jar/json.simple.jar';
Selected (9) IPS ... <-- this is the first **println**
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
at javanolo.CLASS1.main(CLASS1.java:70)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
and i assume this is because it is not finding some of the json simple functions. Why that? I've imported them
// json encode/decode
import org.json.simple.JSONValue;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
and i am sure that i have imported the .jar, because if i run
javac -d . *.java;
error :
import org.json.simple.JSONValue;
^
CLASS1.java:14: error: package org.json.simple does not exist
import org.json.simple.JSONObject;
^
CLASS1.java:15: error: package org.json.simple.parser does not exist
import org.json.simple.parser.ParseException;
^
CLASS2.java:16: error: package org.json.simple does not exist
import org.json.simple.JSONValue;
^
CLASS2.java:17: error: package org.json.simple does not exist
import org.json.simple.JSONObject;
^
CLASS2.java:18: error: package org.json.simple.parser does not exist
import org.json.simple.parser.ParseException;
^
CLASS2.java:148: error: cannot find symbol
public Map<String,String> openAndGetGeoDataByProxy(String ip,String port,int timeout) throws IOException, InterruptedException, ParseException
^
symbol: class ParseException
location: class CLASS2
CLASS2.java:99: error: cannot find symbol
} catch (ParseException ex) {
^
symbol: class ParseException
location: class CLASS2
CLASS2.java:188: error: cannot find symbol
Object jsonObject = JSONValue.parse(line);
^
symbol: variable JSONValue
location: class CLASS2
CLASS2.java:191: error: cannot find symbol
JSONObject jsonArray = (JSONObject)jsonObject;
^
symbol: class JSONObject
location: class CLASS2
CLASS2.java:191: error: cannot find symbol
JSONObject jsonArray = (JSONObject)jsonObject;
^
symbol: class JSONObject
location: class CLASS2
CLASS2.java:194: error: cannot find symbol
JSONObject jsonArray2 = (JSONObject)jsonArray.get("array_result");
^
symbol: class JSONObject
location: class CLASS2
CLASS2.java:194: error: cannot find symbol
JSONObject jsonArray2 = (JSONObject)jsonArray.get("array_result");
^
symbol: class JSONObject
location: class CLASS2
so i know for sure that json-simple.jar is being used when compilation.
The main question is, how can i run CLASS1 and CLASS2 with that .jar file?
Why LINUX based system is so different then the WINDOWS NETBEANS? I mean java is platform independent ...
Ho can i connect NETBEANS to my server trough SFTP so i can code there directly.
Thanks.
EDIT TRIES
i have tried the following commands, none of them works, it gives me the message like i was running the java -help command
try1
java -cp .;jar/json.simple.jar javanolo.CLASS1
try2
java -cp .;'/root/folder/folder/jar/json.simple.jar' javanolo.CLASS1
try3
java -cp .;/root/folder/folder/jar/json.simple.jar javanolo.CLASS1
can anyone help me please (would be better on chat)? i really need this

With this:
java javanolo.CLASS1 -jar 'jar/json.simple.jar'
you run your class and give the rest of the command line, i.e. -jar jar/json.simple.jar as arguments.
What you want is:
java -cp .;jar/json.simple.jar javanolo.CLASS1
or, in a UNIX shell:
java -cp .:jar/json.simple.jar javanolo.CLASS1
Note that your class path is the same in compilation and execution.

Related

Getting Started with Junit. Error while compiling through command line

I have downloaded junit-4.10.jar and hamcrest-2.2.jar to a folder C:/JUnit.
In Environment Variables->System Variables I Have set JUNIT_HOME as C:\JUnit and CLASSPATH as %CLASSPATH%;%JUNIT_HOME%\junit-4.10.jar;.;
Then I created a folder C:/JUNIT_WORKSPACE and added two files TestJunit.java and TestRunner.java.
Now in command line terminal when I enter:
C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java
It throws the below error:
C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java
TestJunit.java:1: error: package org.junit does not exist
import org.junit.Test;
^
TestJunit.java:2: error: package org.junit does not exist
import static org.junit.Assert.assertEquals;
^
TestJunit.java:2: error: static import only from classes and interfaces
import static org.junit.Assert.assertEquals;
^
TestRunner.java:1: error: package org.junit.runner does not exist
import org.junit.runner.JUnitCore;
^
TestRunner.java:2: error: package org.junit.runner does not exist
import org.junit.runner.Result;
^
TestRunner.java:3: error: package org.junit.runner.notification does not exist
import org.junit.runner.notification.Failure;
^
TestJunit.java:5: error: cannot find symbol
#Test
^
symbol: class Test
location: class TestJunit
TestJunit.java:9: error: cannot find symbol
assertEquals("Junit is working fine",str);
^
symbol: method assertEquals(String,String)
location: class TestJunit
TestRunner.java:7: error: cannot find symbol
Result result = JUnitCore.runClasses(TestJunit.class);
^
symbol: class Result
location: class TestRunner
TestRunner.java:7: error: cannot find symbol
Result result = JUnitCore.runClasses(TestJunit.class);
^
symbol: variable JUnitCore
location: class TestRunner
TestRunner.java:9: error: cannot find symbol
for (Failure failure : result.getFailures()) {
^
symbol: class Failure
location: class TestRunner
11 errors
What did I do wrong here? I assume I have set the HOME and Classpath correctly.
The issue is resolved.
I changed the CLASSPATH to %CLASSPATH%;.;%JUNIT_HOME%\junit-4.10.jar;%JUNIT_HOME%\hamcrest-core-2.2.jar;
and after that on running the commands in cmd, I get the correct response:
C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java
C:\JUNIT_WORKSPACE>java TestRunner
true
C:\JUNIT_WORKSPACE>

importing a valid package is causing a java compiler error

I am getting a compiler error when compiling Order.java file even when it contains an import statement for the other packaged class. Im not entirely sure why this is happening but here is a directory tree with some files that I have:
com/my/domain/Order.java
Inside this file are the following package and imports:
package domain;
import utils.MyDate;
com/my/utils/MyDate.java
Inside this file are the following package and imports:
package utils;
Compiler error I get when compiling Order.java :
Order.java:2: error: package com.my.utils does not exist
import com.my.utils.MyDate;
^
Order.java:5: error: cannot find symbol
public MyDate orderDate;
^
symbol: class MyDate
location: class Order
Order.java:16: error: cannot find symbol
public Order(MyDate d, double amt, String c, String p, int q){
^
symbol: class MyDate
location: class Order
Order.java:24: error: cannot find symbol
public Order (MyDate d, double amt, String c) {
^
symbol: class MyDate
location: class Order
4 errors
I am still unsure how to solve this after trying form the comments. Here is some more detail.
Existing Statements in .bash_profile :
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
export CLASSPATH=${CLASSPATH}:/Users/3aCaGa/Desktop/Java-SE-8-Programs/SimplifiedDateClass/com/my
How I am trying to compile? I am going to the java file location in the directory and running command for example :
java Order.java
For more detail on the files that and their exact contents see:
https://github.com/gosem01/Java-SE-8-Programs/tree/master/SimplifiedDateClass/com/my
Your package and import statements do not match your directory structure.
Your Order.class should have:
package com.my.domain;
import com.my.utils.MyDate;
and the Utils.class:
package com.my.utils;
To compile go to the directory where you can "see" the com folder and do:
*nix/MacOS
javac -cp . com/my/domain/*.java com/my/utils/*.java
Windows
javac -cp . com\my\domain\*.java com\my\utils\*.java
Hope it helps

Compiling java class with javac doesn't work

I want to compile my java class like that: javac ResultSet.java
But I get the following error:
ResultSet.java:5: error: package data does not exist
import data.Spieler;
^
ResultSet.java:8: error: cannot find symbol
private ArrayList<Spieler> meineSpieler = new ArrayList<Spieler>();
^
symbol: class Spieler
location: class ResultSet
ResultSet.java:12: error: cannot find symbol
public native Spieler[] getSpieler();
^
symbol: class Spieler
location: class ResultSet
ResultSet.java:18: error: cannot find symbol
public ArrayList<Spieler> getMeineSpieler() {
^
symbol: class Spieler
location: class ResultSet
ResultSet.java:8: error: cannot find symbol
private ArrayList<Spieler> meineSpieler = new ArrayList<Spieler>();
^
symbol: class Spieler
location: class ResultSet
How can I import the spieler class? Should I set the classpath or is there a other way to fix that?
Go one directory up and then compile it with
javac data/JNIResultSet.java
Update:
Ok, your class JNIResultSet is in package model and it uses other classes in package data.
Then your compile command should be as follows:
javac -cp . model/JNIResultSet.java
The -cp . part means, that your classpath includes the current directory. This is the root of your package hierarchy. So the compiler can find the *.java files in package data and compiles them also as needed.
You see, this can be very complicated. For more classes this will be nearly unmanageable. So you should really consider to use a build system like Ant, Maven or Gradle.
use -classpath while compiling the file as
javac -classpath <path-to-dependent-classes> JNIResultSet.java
It is required only when the Spieler is not on classpath!
for more help refer javac oracle documentation

Javac can't find package referenced in classpath

I'm trying to compile a project with javac in windows, but I'm getting a "package x does not exist" error.
Even if the jar file containing them is in the classpath.
Here is the command, I just added line returns to make it readable:
javac
-d bin
-sourcepath src
-cp
.;
lib/gson-2.5.jar;
lib/jruby-complete-9.1.2.0.jar;
lib/lwjgl-platform-2.9.3-natives-windows.jar;
lib/lwjgl-platform-2.9.3-natives-linux.jar;
lib/lwjgl-platform-2.9.3-natives-osx.jar;
lib/jinput-platform-2.0.5-natives-windows.jar;
lib/jinput-platform-2.0.5-natives-linux.jar;
lib/jinput-platform-2.0.5-natives-osx.jar;
lib/lwjgl.jar;
lib/lwjgl_util.jar;
lib/jorbis-0.0.17.jar;
lib/jinput-2.0.5.jar;
lib/gdx-platform-1.9.2-natives-desktop.jar;
lib/gdx-controllers-platform-1.9.2-natives-desktop.jar;
lib/gdx-freetype-platform-1.9.2-natives-desktop.jar;
lib/gdx-1.9.2.jar;lib/gdx-backend-lwjgl-1.9.2.jar;
lib/gdx-controllers-1.9.2.jar;
lib/gdx-controllers-desktop-1.9.2.jar;
lib/gdx-freetype-1.9.2.jar;
lib/jlayer-1.0.1-gdx.jar;
lib/jutils-1.0.0.jar
src/com/azias/awbe/Launcher.java
And here is the error message:
src\com\azias\awbe\Launcher.java:3: error: package com.badlogic.gdx.backends.lwjgl does not exist
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
^
src\com\azias\awbe\Launcher.java:4: error: package com.badlogic.gdx.backends.lwjgl does not exist
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
^
src\com\azias\awbe\Launcher.java:16: error: cannot find symbol
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
^
symbol: class LwjglApplicationConfiguration
location: class Launcher
src\com\azias\awbe\Launcher.java:16: error: cannot find symbol
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
^
symbol: class LwjglApplicationConfiguration
location: class Launcher
src\com\azias\awbe\Launcher.java:27: error: cannot find symbol
new LwjglApplication(new AdvanceWarsBootleg(), config);
^
symbol: class LwjglApplication
location: class Launcher
src\com\azias\awbe\AdvanceWarsBootleg.java:3: error: package com.badlogic.gdx does not exist
import com.badlogic.gdx.Game;
^
src\com\azias\awbe\AdvanceWarsBootleg.java:4: error: package com.badlogic.gdx.graphics.g2d does not exist
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
^
src\com\azias\awbe\AdvanceWarsBootleg.java:6: error: cannot find symbol
public class AdvanceWarsBootleg extends Game {
^
symbol: class Game
src\com\azias\awbe\AdvanceWarsBootleg.java:7: error: cannot find symbol
public SpriteBatch batch;
^
symbol: class SpriteBatch
location: class AdvanceWarsBootleg
src\com\azias\awbe\AdvanceWarsBootleg.java:9: error: method does not override or implement a method from a supertype
#Override
^
src\com\azias\awbe\AdvanceWarsBootleg.java:17: error: method does not override or implement a method from a supertype
#Override
^
src\com\azias\awbe\AdvanceWarsBootleg.java:19: error: cannot find symbol
super.render();
^
symbol: variable super
location: class AdvanceWarsBootleg
12 errors
Using Java 6 or later, the classpath option supports wildcards. Note the following:
Use straight quotes (")
Use *, not *.jar
so you could simplify your javac statement:
javac
-d bin
-sourcepath src
-cp ".;libs/*"
src/com/azias/awbe/Launcher.java
Also depending on the platform the separator is ; (windows) or : (unix).

Java can't find classes in parent package

I'm not sure if this is a classpath problem, a syntax problem, or an access modifier problem. I'm trying to implement packages for the first time in Java and having with the compiler not finding classes in the parent package.
I understand there isn't any hierarchical relationship in package structures and I am explicitly importing parent package classes in the child package class.
The parent package classes' constructors are public.
I am under the impression both directories need to be on the classpath but not sure about it. Either way, I have both dirs on the classpath to be sure.
Directory Structure
home
|
|---java
|
|---src
|
|---com
|
|---inv
|
|---mail
|
|---SendMail.java
|
|---TeradataCon.java
|
|---ExcelWriter.java
CLASSPATH
(mdexter#server) /home/mdexter/java/src/com/inv/mail # echo $CLASSPATH
.:/storage/mdexter/java/lib/*:/usr/java6_64/jre/lib/*:/usr/java6_64/lib/*:/home/mdexter/java/src/com/inv/*:/home/mdexter/java/src/com/inv/mail/*
SendFile.java (stripped down)
package com.inv.mail;
import com.inv.TeradataCon;
import com.inv.ExcelWriter;
public class SendMail
{
public static void main(String[] args)
{
TeradataCon teradata = new TeradataCon(some, args, here);
ExcelWriter xls = new ExcelWriter(some, args, here);
}
}
TeradataCon.java (stripped down)
package com.inv;
public class TeradataCon
{
public TeradataCon()
{
// stuff
}
}
ExcelWriter.java (stripped down)
package com.inv;
public class ExcelWriter
{
public ExcelWriter()
{
// stuff
}
}
Error output
(mdexter#server) /home/mdexter/java/src/com/inv/mail # javac *.java
StrategyVolumes.java:3: cannot find symbol
symbol : class TeradataCon
location: package com.inv
import com.inv.TeradataCon;
^
StrategyVolumes.java:4: cannot find symbol
symbol : class ExcelWriter
location: package com.inv
import com.inv.ExcelWriter;
^
StrategyVolumes.java:14: cannot find symbol
symbol : class TeradataCon
location: class com.inv.mail.StrategyVolumes
TeradataCon teradata = new TeradataCon(
^
StrategyVolumes.java:14: cannot find symbol
symbol : class TeradataCon
location: class com.inv.mail.StrategyVolumes
TeradataCon teradata = new TeradataCon(
^
StrategyVolumes.java:32: cannot find symbol
symbol : class ExcelWriter
location: class com.inv.mail.StrategyVolumes
ExcelWriter xls = new ExcelWriter(;
^
StrategyVolumes.java:32: cannot find symbol
symbol : class ExcelWriter
location: class com.inv.mail.StrategyVolumes
ExcelWriter xls = new ExcelWriter(;
^
6 errors
What I have tried
import com.inv.*; (Shouldn't matter right?)
Compiled parent classes from /home/java/src/com/inv - works
Compiled mail/*.java from /home/java/src/com/inv - doesn't work
I think you've misunderstood the classpath, for starters. You don't put package directories on the classpath - you only put the root of output directories there.
I suggest you compile from the src directory, with the output going to a bin or classes directory. For example, get rid of your CLASSPATH environment variable entirely (it's rarely useful, IME - better to specify it as a command-line option where necessary) and then use something like:
/home/mdexter/java/src # javac -d ../bin com/inv/mail/*.java
Or better, compile everything together, as JB Nizet suggests:
/home/mdexter/java/src # javac -d ../bin `find . -name '*.java'`
(Or use an IDE and/or build tool.)

Categories

Resources