So i wanted to use JSON library in my project but when i try to javac with a classpath of the json.jar
Used this on cmd :
javac -classpath json.jar main.java
and i get this error
error: package org.json does not exist
I dont know why ... then i made a more simple program to try and test if it runs
i get the same error ... I am using Atom for programming in Java .
The really simple program i tried to test :
import java.io.*;
import org.json.*;
public class main {
public static void main(String[] args){
JSONObject obj = new JSONObject();
System.out.println("Hello");
}
}
Related
I am trying to develop a simple Java application and I want it to use some Python code using Jython. I am trying to run a python method from a file and getting this error:
ImportError: cannot import name testFunction
Just trying a simple example so I can see where the problem is. My python file test.py is like this:
def testFunction():
print("testing")
And my Java class:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("path_to_file\\test.py");
interpreter.exec("from test import testFunction");
So it can correctly find the module but behaves like there is no function called testFunction inside.
Execute script files as follows.
interpreter.execfile("path/to/file/test.py");
If you have functions declared in the script file then after executing the above statement you'll have those functions available to be executed from the program as follows.
interpreter.exec("testFunction()");
So, you don't need to have any import statement.
Complete Sample Code:
package jython_tests;
import org.python.util.PythonInterpreter;
public class RunJythonScript2 {
public static void main(String[] args) {
try (PythonInterpreter pyInterp = new PythonInterpreter()) {
pyInterp.execfile("scripts/test.py");
pyInterp.exec("testFunction()");
}
}
}
Script:
def testFunction():
print "testing"
Output:
testing
When I try to run a java program in the cmd it displays the messege "Could not find or load main class".
The problem occurs when there is a package involved in the program, otherwise it works fine.
The code is:
package myPackage;
public class index {
public static void main(String [] args){
System.out.println("Hello World");
}
}
I've tried writting in cmd: javac (name of package) . Class name, but still doesnt work.
The issue was that the class path needs to be set for each command (javac and java):
Attempted Steps
Compile index.java from the top_level. do not use sub package.
$javac -cp . importpackage/subpackage/index.java
I'm a relative Java newbie so apologies if the question appears somewhat basic. I've googled high and low for an answer here and I'm not finding anything that's helping.
Problem:
Whilst I'm able to integrate external packages into my Java programs from an IDE environment, I am trying to do run a very basic program from the command line that calls on a separate, basic package file that I have written - and am simply doing all this as I want to have a bottom-up understanding of how package files are related to a main program by Java.
I have a program that sits on my desktop named MyProgram.java:
import org.somepackage;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
Still on the Desktop, I then have another folder which I've named src, inside of which I have created the necessary subfolders corresponding to the package name, i.e. ./src/org/somepackage - and in this location, I have the Human.java file which defines the Human class with the following contents:
package org.somepackage;
public class Human {
public void scream() {
System.out.println("I exist!!");
}
}
I then created a classes folder, again on the Desktop, and ran the following compile command on the command line:
javac -d ./classes/ ./src/org/packagename/Human.java
This ran fine and created - as expected - the Human.class file within the ./classes/org/packagename/ location.
However, where I fall down is when I then try to compile MyProgram.java on the command line, i.e.
javac -cp ".:./classes/" MyProgram.java
As you'll see, my class path contains a reference to the current location (".") for the MyProgram.java file, and it contains a reference to the classes folder ("./classes/") which is the base location for the org.somepackage package inside whose subfolders (./classes/org/somepackage/) on can find the Human.class file.
At this stage, I was simply expecting the java engine to compile MyProgram.java into the program MyProgram.class - but, instead, I get an error:
MyProgram.java:1: error: package org does not exist
I've been following the instructions listed here:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html
and I don't appear to be deviating from the instructions - yet I'm unable to locate an explanation on Stackoverflow or anywhere else as to a possible reason for this compile failure. If anyone has an idea, your help would be very much appreciated.
Thanks,
Your mistake is here
import org.somepackage; <--
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
you forgot to import class actually, you need to write this name
import org.somepackage.Human; import all package content import org.somepackage.*; or write full qualified name of class in your code
org.somepackage.Human myHuman = new org.somepackage.Human();
myHuman.scream();
correct mistake:
import org.somepackage.Human;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
after that compile your Human.java by this command:
javac -d classes Human.java
and MyProgram.java
javac -d classes -cp "classes" MyProgram.java
and run MyProgram by
java -cp "classes" MyProgram
I'm pretty new to Java and I'm sure this is a noob question but I really can't get this to work.
I have a simple java file with some dependencies that looks like this:
package myFilePackage;
import java.io.*;
import java.nio.*;
import java.util.*;
public class myclass{
public static void modifySomeFile(arg1, arg2, ...){
//Function that uses the above dependencies
...
}
public static void main(String[] args){
for( arg : args){
System.out.println(arg);
}
//my code works fine up to here!
modifySomeFile(arg1, arg2, ...);
}
}
Basically, what happens is that it prints all the arguments just fine when I call it from the command prompt, but never calls the modifySomeFile function.
This is how I call it from the prompt:
cd C:\path\to\file java -jar myjar.jar arg1 arg2
Here's my manifest file (I read somewhere that it might be the problem)
Manifest-Version: 1.0
Class-Path: .
Main-Class: myfile.myClass
Here's the path to my class:
myfile/myClass.class
Also, there's only one class in my jar file.
I am trying to create a package of library functions for a main Java program but I am have some issues.
I don't know much about about Java packages and I am going through some documentary online.
I have created my directory as such
./Program/Program.java
./Program/TestFunc.java
./Program/classes/library/
The contents of TestFunc.java are
package library;
public class TestFunc {
public void message01() {
System.out.println("called message01");
}
public void message02() {
System.out.println("called message02");
}
}
I compiled it as I read in the documentation
javac -d ./Program/classes TestFunc.java
Which gives me
./Program/classes/library/TestFunc.class
Then I try to call it in Program.java
import library.*;
public class Program {
public static void main(String[] args) {
System.out.println("Starting Script");
}
}
When I try to compile using
javac -d ./Program/classes Program.java
I get the error
package library does not exist
Why is this?
You've used -d which says where to put the output, but you haven't told it that the same directory should also be used for input on the classpath. Use the -cp option for that:
javac -d classes -cp classes Program.java
(It's not clear whether you're trying to do this from inside the Program directory, or above it - your source filename appears to be inside the Program directory, but you're specifying the output directory as if you were in the directory above...)