This is my directory layout:
~ koraytugay$ ls -1 biz/tugay/hellospring/
Bike.java
Car.java
Vehicle.java
VehicleApp.java
VehicleService.java
beans.xml
So I am in the root folder and my .java files are in biz/tugay/hellospring/
Code for VehicleApp:
package biz.tugay.hellospring;
/* User: koray#tugay.biz Date: 29/06/15 Time: 15:16 */
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class VehicleApp {
public static void main(String[] args) {
ApplicationContext applicationContext
= new ClassPathXmlApplicationContext("biz/tugay/hellospring/beans.xml");
VehicleService vehicleService = (VehicleService) applicationContext.getBean("vehicleService");
vehicleService.driver();
}
}
and VehicleService:
package biz.tugay.hellospring;
/* User: koray#tugay.biz Date: 29/06/15 Time: 15:10 */
public class VehicleService {
private Vehicle vehicle;
public void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
}
public void driver(){
System.out.println(vehicle.drive());
}
}
Also in my home directory I have the following jar files:
~ koraytugay$ ls -1 *.jar
spring-aop-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-security-core-3.2.5.RELEASE.jar
I tried several variations however I was not successful with any of them. One example:
~ koraytugay$ javac -cp .:/biz/tugay/hellospring biz/tugay/hellospring/VehicleApp.java
biz/tugay/hellospring/VehicleApp.java:4: error: package org.springframework.context does not exist
import org.springframework.context.ApplicationContext;
^
biz/tugay/hellospring/VehicleApp.java:5: error: package org.springframework.context.support does not exist
import org.springframework.context.support.ClassPathXmlApplicationContext;
^
biz/tugay/hellospring/VehicleApp.java:10: error: cannot find symbol
ApplicationContext applicationContext
^
symbol: class ApplicationContext
location: class VehicleApp
biz/tugay/hellospring/VehicleApp.java:11: error: cannot find symbol
= new ClassPathXmlApplicationContext("biz/tugay/hellospring/beans.xml");
^
symbol: class ClassPathXmlApplicationContext
location: class VehicleApp
4 errors
I have the .jar files in the folder I am executing javac. Why the compiler is unable to find ClassPathXmlApplicationContext ?
you either need to specify every jar
java -cp ./spring-aop-3.2.5.RELEASE.jar ; ./spring-beans-3.2.5.RELEASE.jar ;./spring-context-3.2.5.RELEASE.jar ; ./spring-security-core-3.2.5.RELEASE.jar
or use a wild card
java -cp *.jar; /otherpath
but thats only works with java 6 up
Related
I created a simple tic-tac-toe game, and put some functions on another package, so i can import them on my Main function.
When i run on eclipse IDE, i have no problem, but when i try run on windows CMD i get the following error: console log error
Here is my folder structure: folder structure
initial code of Main class, with imports:
package com.github.vitucomment.main;
import java.io.IOException;
import java.util.Map;
import com.github.vitucomment.players.PlayersFunctions;
import com.github.vitucomment.utils.BoardFunctions;
import com.github.vitucomment.utils.GameRulesFunctions;
import com.github.vitucomment.utils.ScoreFunctions;
public class Main {
Console log error:
C:\Users\victo\eclipse-workspace\Java-JogoDaVelha\src\com\github\vitucomment\main>java Main.java
Main.java:6: error: package com.github.vitucomment.players does not exist
import com.github.vitucomment.players.PlayersFunctions;
^
Main.java:7: error: package com.github.vitucomment.utils does not exist
import com.github.vitucomment.utils.BoardFunctions;
^
Main.java:8: error: package com.github.vitucomment.utils does not exist
import com.github.vitucomment.utils.GameRulesFunctions;
^
Main.java:9: error: package com.github.vitucomment.utils does not exist
import com.github.vitucomment.utils.ScoreFunctions;
^
Main.java:82: error: cannot find symbol
public static PlayersFunctions playersFunctions() {
^
symbol: class PlayersFunctions
location: class Main
Main.java:86: error: cannot find symbol
public static ScoreFunctions scoreFunctions() {
^
symbol: class ScoreFunctions
location: class Main
Main.java:90: error: cannot find symbol
public static BoardFunctions boardFunctions() {
^
symbol: class BoardFunctions
location: class Main
Main.java:94: error: cannot find symbol
public static GameRulesFunctions gameRulesFunctions() {
^
symbol: class GameRulesFunctions
location: class Main
Main.java:17: error: cannot find symbol
PlayersFunctions playersFunctions = playersFunctions();
^
symbol: class PlayersFunctions
location: class Main
Main.java:83: error: cannot find symbol
return new PlayersFunctions();
^
symbol: class PlayersFunctions
location: class Main
Main.java:87: error: cannot find symbol
return new ScoreFunctions();
^
symbol: class ScoreFunctions
location: class Main
Main.java:91: error: cannot find symbol
return new BoardFunctions();
^
symbol: class BoardFunctions
location: class Main
Main.java:95: error: cannot find symbol
return new GameRulesFunctions();
^
symbol: class GameRulesFunctions
location: class Main
13 errors
error: compilation failed
I don't know how to fix it, before change to packages it was working without problems.
CMD run project
make a file x.bat
copy Code change you config
#echo on
#title E:\projectPath
#set dir_path=E:\libPath
#setlocal EnableDelayedExpansion
#for %%i in (%dir_path%\*.jar,%dir_path%\lib\*.jar) do (
#set other_path=!other_path!;%%i
)
set classpath= %other_path%
#java package.Main
I executed my java file like this:
javac -cp ".:/root/Desktop/karim/software/UIMA/yagogit/yodaqa/gradle/wrapper/gradle-wrapper.jar" LATByFocus.java
which gives error like:
LATByFocus.java:5: package org.apache.uima does not exist
import org.apache.uima.UimaContext;
^
LATByFocus.java:6: package org.apache.uima.analysis_engine does not exist
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
^
LATByFocus.java:7: package org.apache.uima.fit.component does not exist
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
^
LATByFocus.java:8: package org.apache.uima.fit.util does not exist
import org.apache.uima.fit.util.JCasUtil;
^
....
LATByFocus.java:36: cannot find symbol
symbol: class JCasAnnotator_ImplBase
public class LATByFocus extends JCasAnnotator_ImplBase {
^
LATByFocus.java:37: cannot find symbol
symbol : class Logger
location: class cz.brmlab.yodaqa.analysis.question.LATByFocus
final Logger logger = LoggerFactory.getLogger(LATByFocus.class);
^
LATByFocus.java:39: cannot find symbol
symbol : class UimaContext
location: class cz.brmlab.yodaqa.analysis.question.LATByFocus
public void initialize(UimaContext aContext) throws ResourceInitializationException {
^
LATByFocus.java:39: cannot find symbol
symbol : class ResourceInitializationException
......
So I wanted to try considering pom.xml along with jar file in command line - pom.xml
Is it possible?
I am trying to execute these files https://github.com/brmson/yodaqa/tree/master/src/main/java/cz/brmlab/yodaqa/analysis/question
in project : yodaqan
In my Win7 machine I have added in the CLASSPATH like this:
CLASSPATH=D:\Dev\Java;C:\Program Files (x86)\Java\jre1.8.0_20\lib\ext\QTJava.zip.
In my directory tree I have created a D:\Dev\Java\abc folder and placed a filed called Address.java that contained this code:
package jme;
public class NewClass {
}
Having done that, I created a project that looks like this:
package javaapplication1;
package abc; // << Error
public class JavaApplication1 {
public static void main(String[] args) {
abc.Address address; // << Error
System.out.println("Jaaaa");
}
}
Why the abc package, when located in the CLASSPATH, is not recognized?
You need to use import ...
package javaapplication1;
import abc.*; // No error if you have the package in the classpath ...
public class JavaApplication1 {
public static void main(String[] args) {
Address address; // No need to prefix with abc, since you imported it before ...
System.out.println("Jaaaa");
}
}
You can't declare double package for a class in Java, and I think that is not what you really want to do ...
To import correctly the classes contained in the abc package make sure to have the abc package and their related classes in your classpath ...
Sorry guys for the horrendous mishap, I am kinda new here, but I'm a quick learner.
The CLASSPATH reads: D:\Dev\Java\abc;C:\Program Files (x86)\Java\jre1.8.0_20\lib\ext\QTJava.zip
Can't find a solution to this problem.
Guitar2.java:
public class Guitar2
{
private String serialNumber;
public Guitar2(String serialNumber)
{
this.serialNumber = serialNumber;
}
public String getSerialNumber()
{
return serialNumber;
}
}
Inv2.java:
import java.util.List;
import java.util.LinkedList;
public class Inv2
{
private List guitars;
public Inv2()
{
guitars = new LinkedList();
}
public void addGuitar(String serialNumber)
{
Guitar2 guitar = new Guitar2(serialNumber);
guitars.add(guitar);
}
}
Both files are in the same directory, both are 755 and the directory is in the classpath. I get the error message:
[machine]me # directory $ javac Inventory.java
Inventory.java:18: cannot find symbol
symbol : class Guitar
location: class Inventory
Guitar guitar = new Guitar();
^
Inventory.java:18: cannot find symbol
symbol : class Guitar
location: class Inventory
Guitar guitar = new Guitar();
^
2 errors
I read that if a file is in the same directory, classes from it can be used in other files in the same directory without any import statements. What's the problem here?
POST EDIT Output when using the above code:
[me]machine # ricks $ javac Inv2.java
Note: Inv2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
I get the .class files of both .java files.
Run javac Guitar.java
and only then (after it has been compiled and a Guitar.class was created) run javac Inventory.java
Try to compile Guitar.java first. then run your cmd . or try this : javac *.java
Make sure both classes have the same package. Even if they are in the same directory, a different package could cause this problem. Type the same package or use an import and then try compiling with javac *.java
I have in one folder /projects/A/Test.java
with the following code:
public class Test {
public static void t() {
System.out.println("Hey");
}
}
and in folder#2 /projects/
with the following code:
public class Test2 {
public final static void main(String[] args) {
Test t = new Test();
t.t();
}
}
I compile the first one with: javac Test.java
I compile the second one with javac Test2.java -cp ./A
Now when I try to run Test2: java Test2, I receive an error:
Exception in thread "main" java.lang.NoClassDefFoundError: Test
How can I run Test2, which calls Test from a subfolder?
If your classes are in different places you have to set the classpath when running, just like when you compile.
java -cp .:./A Test2 # On Unix use :, on Windows ;
The problem is that in class test you had to specify package in the beginning of the file.
package A;
And then import it in your Test2 class.
import A.Test
you don't have to set package and import Test if both locations project and project/A are in your classpath