I made a library in java to make calling maps from google maps easier and faster but I can't seem to import the jar file correctly. I followed the answer I found on this page How to create my own java library(API)? but it seems I am still doing something wrong.
I named the package in the library com.googleMaps and then I exported the .jar file and then added that .jar file into my build path of another project. I then made a class, imported com.googleMaps.StaticMap; Which gave me an unused library warning. Finally inside my main method I called DisplayMaps("string"); which is a method inside of StaticMap; But it gives me an error saying the Method does not exist but the unused warning on the import went away.
Error is: The method DisplayMap(String) is undefined for the type MapTest1
Code:
import com.googleMap.StaticMap;
public class MapTest1
{
public static void main(String[] args)
{
DisplayMap("A Url Goes here"); // This is where im getting the error
}
}
You should call it with StaticMap.DisplayMap("A Url Goes here");. That's assuming it's a static method.
When calling a static method, you have to specify the class it belongs to, unless you are calling it from another method of the same class.
If it's not a static method, you have to create an instance of StaticMap before calling the method :
StaticMap map = new StaticMap();
map.DisplayMap("A Url Goes here");
Related
I need to use a .jar library, given by my teacher, to code for my Java class.
I am using VS Code, with the Java Extension Pack installed, for Java Project Management.
Can someone please explain me step by step how is it possible to import the .jar library, in order to use the classes defined by my teacher.
I have tried to copy the .jar in the lib folder and then add the reference, but it still did not work. I also know that I have to declare the classpath, but when I create the Java Project the .classpath file is not created automatically.
Thanks already!
First you should examine the classes in .jar file. Then you should load that class as,
Class<?> c1 = Class.forName("java.lang.String");
Then after you can use that class by calling that Class reference type variable.
See this example as well,
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
// get the Class instance using forName method
Class c1 = Class.forName("java.lang.String");
System.out.print("Class represented by c1: "+ c1.toString());
} }
Try to understand the code and implement proper solution to your project.
Good Luck.
The file "HelloDemo.java" path is "/test/hello/HelloDemo.java"
package test.hello;
public class HelloDemo {
public static void main(String[] args) {
System.out.print("Hello!!");
}
}
when I "run" it, an error occurred.
Building HelloDemo.java and running HelloDemo
Error: Could not find or load main class HelloDemo
Then, I changed the code.
//package test.hello;
public class HelloDemo {
public static void main(String[] args) {
System.out.print("Hello!!");
}
}
when I "Run" it, code success output.
Building HelloDemo.java and running HelloDemo
Hello!!
This is the screenshot about the "Run".
I fixed an error, but I don't konw why, I need help, Thank you!
If I want to keep the package uncomment, How to fix it?
That's because you probably changed the location of your file after running it once already. Hence, the running configuration should change to look for the new test.hello.HelloDemo class inside the built jar and not for HelloDemo anymore (which was probably in the default package, initially). What is your IDE?
Remark: This is not because you changed the location of your file that the classpath changed, and vice-versa.
On IntelliJ, you should do this: https://www.jetbrains.com/help/idea/creating-and-editing-run-debug-configurations.html
Create a package using your IDE and add your class to it. Package name will be appended to top automatically.
Reguardless of IDE, folder structure should match package structure, your problem could be here.
A class's name is actually the package plus the class name. You cannot run HelloDemo in your first case, because that is not the class name. The class name is test.hello.HelloDemo.
By commenting out the package, you've essentially renamed the class to HelloDemo, so it runs.
In addition, when running the class with main, you must be in the correct location. For instance, if the class is test.hello.HelloDemo, your folder structure will be /test/hello/HelloDemo.java.
You must be in / and run test.hello.HelloDemo from there.
It's a simple class and I am a beginner with Java.
I don't know why this code is not running and why it gives an error :
Could not find or load main class
class tuto{
public static void main(String[] args){
System.out.println("Hello World");
}
}
There are a couple things which jump out at me when I look at your question.
The first thing is that you have unresolved compiler errors. If you see that red 'x' on the Problems tab, you should fix all the errors there before trying to run anything.
The second thing is that your class name doesn't match the file name in which it is defined. For public classes the name of the class and the name of the file must match, and while your class isn't public, this is a widely followed Java convention and you will confuse people if you don't follow it.
As to your actual question, my best guess is that you have placed your class into a package and not declared it as such in your source code. If you go look at the Problems tab, it will tell you what is wrong and (often) how to fix it.
I can approximate your error message if I do the following:
In this case, I have an error over in the Problems tab complaining about the declared package.
Check to see if you have something similar:
If you do, you can right-click the error message and select "Quick Fix", and eclipse will pop up a dialog offering to add the package declaration for you:
In your code there is a compile error, that is because Syteme change it to System
Syteme.out.println("Hello World");
should be
System.out.println("Hello World");
P.S
And in Java when you have a public class in a file, then file name must be that class name. It is a must. Otherwise you will get an error.
If you have this class in a package then you must specify the package declaration first
e.g
package abc;
System.out.println not Syteme.out.println.
In Java (as somebody has already pointed) the name of the file should be of the same name of the main class within the same file.
Moreover, you should also declare an array using this syntax array_type [] array_id and not array_type array_id [].
There might be a couple of problems:
If the class is in a package, make sure you specify it. eg: package com.pak;
The class with main method always needs to be public. public class apples{}
I've been running different individual Java .java files in the Netbeans IDE by right-clicking the .java files themselves in the Project Explorer of Netbeans (the portion normally at the upper left part of Netbeans).
However, i've been googling on how to make a class file run another class file using code, but to no avail.
I have a project named "loadanotherfile" with 2 files, namely: Loadanotherfile.java and otherfile.java
I'm trying to make Loadanotherfile.java run otherfile.java, but I'm not exactly sure how. I read about Classloaders and URLClassloaders however these methods don't seem suitable for my purpose of running another .java file.
Below is the code of the 2 files i mentioned.
Loadanotherfile.java
package loadanotherfile;
public class Loadanotherfile {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
// TODO code application logic here
}
}
otherfile.java
package loadanotherfile;
public class otherfile {
public static void main(String args[])
{
System.out.println("This is the other file.");
}
}
I have a feeling that the task has something to do with using the "import" syntax (namely something like import loadanotherfile.* but even if my guess is correct, I'm still not sure on how to make my Loadanotherfile.java run otherfile.java using code.
How can I load otherfile.java using Loadanothefile.java?
Cheers
In Loadanotherfile.java
otherfile.main(args);
Compile the two together, and then from Loadanotherfile,
otherfile.main(args);
will do the trick. You don't need to import since you're in the same package. Note the linked tutorial.
I would investigate (however) class instantiation, and creating an instance of a new class to invoke upon. Invoking static methods from static methods isn't very OO.
Try This:
className.main(Args){
}
This works! ive tested it myself.
Check the public void main line. If there IOException and not there then insert
in Loadanotherfile.java
use this
otherfile.main(args);{
}
Some weird behavior in Netbeans 7.0. Ostensibly something went wrong when I created a class, because now no matter what project I am in, if I create a class named "RainbowBall" in a package called "gamesandbox.agents" (even if I just created the package fresh), it compiles fine, but the debugger gives me "Thread main stopped" when I call the RainbowBall constructor.
Stripped down example from a freshly created project:
//RainbowTest.java
package rainbowtest;
import gamesandbox.agents.RainbowBall;
public class RainbowTest
{
public static void main(String[] args)
{
RainbowBall r = new RainbowBall();
System.out.println(r.toString());
}
}
/*---------------*/
//RainbowBall.java
package gamesandbox.agents;
public class RainbowBall
{
public RainbowBall() {};
}
Again, this compiles fine, but the debugger acts like RainbowBall is an unresolvable symbol ("Thread Main Stopped at RainbowTest.java:10").
If I use any other class name (ex. "RainbowBall2") or any other package name I do not get this error. It happens in freshly created projects as well as old ones, and even when no outside libraries/jars/packages are being used in any way.
I'll probably just change the name or try updating to the latest NetBeans, but it would be good to understand what's going on. The IDE has clearly stored the name of the class somewhere permanent and project-agnostic, and is refusing to work with RainbowBalls like some kind of homophobe.
The output message you gave sounds like NetBeans thinks there is a breakpoint in the class. I'm not sure why it would be global to every project, though.