public class Main {
public static void main(String[] args) {
}
}
i want to add package how to add it in intellij idea. Is the problem in the beginning of creating a new project ?
Lets assume you have an existing class, and you want it to be in a package. This is called refactoring
Right click on your class name (Main in this case)
In the context menu expand the "refactor"
In the refactor menu select "move to ..."
Then a dialog should appear that lets you enter the name of the package you would like to move your class to.
If it is a new package, it will create a folder and move the source file there.
Your code in file named Main.java
Just add two directories named com and codewithmosh, respectivly inside each other and then move your Main.java class inside it.
After that you can add package statement to your code:
package com.codewithmosh;
public class Main {
public static void main(String[] args) {
}
}
Direcory structure for your project is:
────project
└───com
└───codewithmosh
└───Main.java
Related
In
C:\Dev\Java\MyFirstJavaClass\
I have a file named MyFirstJavaClass.java, so the path looks like this
C:\Dev\Java\MyFirstJavaClass\MyFirstJavaClass.java
and in "C:\Dev\Java\MyFirstJavaClass" I have a subdirectory called 'src'.
So the path looks like this:
C:\Dev\Java\MyFirstJavaClass\src
In this directory I want to put a file named "MyName.java", so the path would look like this
C:\Dev\Java\MyFirstJavaClass\src\MyName.java
In MyFirstJavaClass.java I have this code
public class MyFirstJavaClass{
public static void main(String[] args){
System.out.println("You have your first Java program running!!\n\n");
}
}
This file compiles, but I'd like to add the files in ./src into my MyFirstJavaClass.java file, how do I #include the file(s) in 'src' in MyFirstJavaClass.java?
Thanks
1) You should check some tutorials about packages and classpath .
https://www.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html
http://www.tutorialspoint.com/java/java_packages.htm
2) You should create the package and structure as described below
package dev.java.myfirstjavaclass;
public class MyFirstJavaClass {
}
It will create your class file inside the folder structure dev/java/myfirstjavaclass in your classpath, Also as per the java naming conventions packages name should be in lower cases.
package dev.java.myfirstjavaclass.src;
public class MyName{
}
It will create your class file inside the folder structure dev/java/myfirstjavaclass/src inside your classpath.
Now as you have called #include the file. You have to use the import statement to include the file where you want to use it.
import dev.java.myfirstjavaclass.src.MyName;
public class MyFirstJavaClass {
public static void main(String[] args) throws IOException{
MyName test=new MyName();
}
}
3) Also look some tutorials on objects and java concepts about how to use them.
I am extremely new in Java and I couldn't get one thing straight. I have E:\Java\ACP\Cricket\ directory where I have a Main.java, Player.java and CricPlayer.java. Now I am inheriting Player class into CricPlayer.
I have Main.Java as
import Java.ACP.Cricket.*;
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello.");
}
}
CricPlayer.java As...
import Java.ACP.Cricket
public class CricPlayer extends Player
{
}
I need to use objects of CricPlayer in Main.java. As I mentioned earlier I am real new to Java so there might be some really obvious mistakes that I don't know about. But the question is how can I create a package for use in CricPlayer.java and Main.java? Because each time I compile the code using CMD it says "CricPlayer.java:6: error';' expected import java.ACP.Cricket.*" and points ^ to *. I don't know if I haven't created the Package properly or if it's some syntactical issue.
you need to add package declaration in the beginning of your code files. And you don't need imports if all the classes reside in the same package. BTW, good coding convention in java recommends that all letters in package name are lowercase :)
package Java.ACP.Cricket;
public class Main
{
// Your class contents
}
in the class below you shourd write a package (folder in project where file is situated)
for example
package ACP.Cricket;
You forgot the ; at your import in CricPlayer class:
import Java.ACP.Cricket.*;
Now you should be able to create CricPlayer Objects in your Main Class.
Also I don't know if "Java" in your package name is supposed to be capitalized or not. Seems like the compiler is expecting a lower case 'J' from your naming convention
Here's an alternative to your Programs.I've verified this myself, so you can be assured that it will work.
Important thing you need to know, is that any file can be compiled only if you have a main method for that file(ie in that class). so here's a detailed 'How to do it':
PlayerDemo.java:
package Java.ACP.Cricket;
class Player{
//class members & methods
}
class PlayerDemo{
public static void main(String[] args){
System.out.println("Player Class");
}
}
CricPlayerDemo.java:
package Java.ACP.Cricket;
class CricPlayer extends Player{
// class members & methods
}
class CricPlayerDemo{
public static void main(String[] args){
System.out.println("CricPlayer Class");
}
}
Main.java:
package Java.ACP.Cricket;
class Main{
public static void main(String[] args){
System.out.println("Main Class");
//use other class objects here directly.
}
}
In E: , do following stepwise:
1> javac PlayerDemo.java (this creates Player.class & PlayerDemo.class)
2> copy Player.class to Cricket folder.
3> javac CricPlayerDemo.java (this creates CricPlayer.class & CricPlayerDemo.class)
4> copy CricPlayer.class to Cricket folder.
5> javac Main.java
6> copy Main.class to Cricket folder.
7> now,also in E:, java Java/ACP/Cricket/Main
Instead of compiling from E:, you can also compile from Cricket,to avoid copying class files,but it wont work for CricPlayerDemo.java, as CricPlayer extends Player(whose location is acc to package) so you'll have to compile CricPlayerDemo from E:
Vincenzzochi is right, packages are lower case (but it should not break compilation).
And you need to declare the package at the begining of all your classes :
package java.acp.cricket;
And while all your source files are in the same package, you don't have to import anything!
import stanford.karel.*;
public class MidpointFindingKarel extends SuperKarel
{
public void run()
{
move();
}
}
This is my example code.the method run() is inherited from the SuperKarel class.
All i want is to run this code and codes like this as a java application.but when i click on the 'Run as', i see only 'run as java applet' there.how to get the option of 'run as java application there'.i know how to create a run configuration but when i do,that configuration is being applicable only to a particular java file.how to run codes like these without creating a run configuration everytime for a new java file??
You need to declare main() as
public static void main(String[] args)
For instance you might change your class to
public class MidpointFindingKarel extends SuperKarel {
public void run() {
move();
}
public static void main(String[] args) {
MidpointFindingKarel mfk = new MidpointFindingKarel();
mfk.run();
}
In order to do that, you need a
public static void main(String[] args) {
}
method in your class. It may be that MidpointFindingKarel is used by the main class which is defined somewhere else. In that case, search for it and run that class.
In essence, Eclipse populates the run menu based on the current class that has focus in the editor.
Once, you have the run configuration set up, you can of course simply right click on the run menu dropdown box and select the class to execute (typically, top-left on the toolbar).
You need to add a main method in your class.
Run as Java Application is applicable only to classes having public static main method. Change the code to add main method. You will find the option of run as.
My file name is Temp.java and inside it I have this. I'm using eclipse IDE
/*package*/ class Test {
public static void main(String args[]) {
System.out.println("test");
}
}
So I was unable to run this as java application. I change my class name to Temp
class Temp {
....
}
Now I can. Can someone explain me why ?
This is probably a limitation of Eclipse. The code runs well from command line.
As I understand, you are trying to embed your unit tests in the same file with the class under test. This is a nice idea and I totally concur with it. You can read more about how you can succeed in Ben J. Christensen's blog post. Generally, he suggests placing the tests in a static inner class, not a standalone class in the same file.
An example from the Netflix Hystrix framework: HystrixCircuitBreaker.UnitTest
The code below, located in Temp.java, compiles and runs fine with Netbeans:
class Whatever {
public static void main(String[] args) {
System.out.println("hello");
}
}
The problem is with eclipse, i think you are trying to run using right click -> run as -> Java Application, unfortunately eclipse is not showing this option if the class is not public.
But you can still run the class using Alt+Shift+X,J.
Its not the problem with Java, its with Eclipse.
The name of the file should be the same as the class name which is public and has the main() method. In your first case the file name Temp.java will compile and will create Test.class file not Temp.class because there is no Temp class declared in your file.
after .class file is created , run it with java Test
so here's an example
//Filename abc.java
public class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
the output
abc.java:1: class hi is public, should be declared in a file named hi.java
public class hi
^
1 error
but if you do this
//Filename abc.java
class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
it will create hi.class file so
D:\>java hi
Hell
The class (which main should be run) inside the .java file must have the same name as the file. If the class is not public (as in your case) the class will compile but it can't be run since Eclipse tries to load the class according to the file name.
I have
public class TestApp { // TestApp.java
public static void main(String[] args) throws Exception {
TestClass x = new TestClass(); // compiler error
}
}
and
public class TestClass { // TestClass.java
public TestClass() {
}
}
TestClass was created by right clicking the project and clicking Add Class.
I'm using NetBeans
Assuming you didn't declare a package for either class:
The reason it worked after you placed them in the same folder is that they both became part of the same package.
This is the default unnamed package. All source files without a package declaration, in the same source folder, belong to that folders default package. Also, they cannot be imported for use in other packages.
It's useful for small test apps, but you should strive to put all classes in a proper package.
classes are known by their name, their full name !
com.myapplication.mymodule.Myclass
you can use the short part provided you :
use an import statement : import com.myapplication.mymodule.Myclass
use the short name in a class with same package
from your sample, before using import, you can to use full name :
com.test.TestClass x = new com.test.TestClass(); // change "com.test" for the real package