I am trying to compile a small program. During compilation, it tries to look at wrong path for java util libs. Below is the compilation output
javac -Xlint:unchecked adssim/util/XMLParser.java
adssim/util/XMLParser.java:3: error: cannot access util
import java.util.Vector;
^
bad source file: ./java/util.java
file does not contain class java.util
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
File loation
/apps/oasis/adarsh/adssim/util>ls -lrt XMLParser.java
-rwxrwxrwx 1 oasis oasis 1258 Apr 8 15:49 XMLParser.java
Classpath
/apps/oasis/adarsh/adssim/util>echo $CLASSPATH
:/apps/oasis/java/classes12.jar:/apps/oasis/java/ifxjdbc.jar:/apps/oasis/java/log4j-1.2.14.jar:/apps/oasis/java/commons-io-2.5.jar:/apps/oasis/adarsh/adssim:/apps/oasis/adarsh/adssim/util:/apps/oasis/adarsh/adssim/util/crypto:/apps/oasis/adarsh/adssim/channel:/apps/oasis/adarsh
File Source
package adssim.util;
import java.util.Vector;
public class XMLParser
{
public static Vector getXMLTagValue(String xml, String section) throws Exception
{
String xmlString = new String(xml);
Vector v = new Vector();
What am i doing wrong guys ?
Is there a file on your disk that is actually called util.java in a directory called java? Maybe you have created such a file by mistake?
This part of the error message makes it sound like that:
bad source file: ./java/util.java
Related
To start things off, I am entirely new to Java. I'm a C#/Powershell guy. A client at my IT Firm had an issue with a java program that they were executing on a daily basis that was having issues. According to Windows, the original program was written in April of 2011. I was able to unzip the file and pulled out all of the java files. I then rebuilt the program's structure in NetBeans and am getting ready to start editing. However, each *Test.java file is unable to import junit.framework.TestCase. In the original program file, each of these files were in the same folders as their associated files. From what I can tell, that is not best practices but it was the folder structure I found in the *.jar file I pulled them from. i.e.:
+ Source Packages
|
+--+ Folder
|
+--Example.java
|
+--ExampleTest.java
This leads me to 2 potential issues:
Reading similar threads regarding junit.framework "does not exist", there is mention of adding the junit.jar to the POM or adding the dependency to maven. For NetBeans, how do I do this? Using the "Add Dependency" menu, I am unable to find a "junit.framework" and there is 125,000 results for junit that I am unsure which one I need. Any insights? At the time of the original program's writing, v3 and v4 were both released, although v3.8.1 remained in use for some time beyond the adoption of v4.
For its use-case, see below. I assume all the errors are related to the junit import, so I included them as comments.
package com.example.program;
import java.util.Properties;
import junit.framework.TestCase;
/* Import files specific to program */
public class ExampleTest extends TestCase { //Cannot find symbol (class) "TestCase"
private Properties config = null;
#Override //Error: method does not override or implement a method from a supertype
/* SetUp function/method w/out any issues, creates config Properties object */
public void testExample(){
String line = "*"; // some csv line being parsed
CSVLine csvLine = new CSVLine(line, config);
assertEquals(/* does stuff */); // Error: cannot find "symbol" (method) "assertEquals"
assertTrue(/* does stuff */); // Error: cannot find "symbol" (method) "assertTrue"
assertTrue(/* does stuff*/); // Error: cannot find "symbol" (method) "assertTrue"
}
}
Do I need to move these Test.java files into a folder under the Test Packages section of the POM? Why would the original program have them in the same directory as their counterparts? Does some aspect of compiling/building move them to the same location?
Here is the thing: I am trying to run the example program in the joda-time project.
The start of the Examples.java file looks like this:
package org.joda.example.time;
import java.util.Locale;
import org.joda.time.DateTime;
import org.joda.time.Instant;
/**
* Example code demonstrating how to use Joda-Time.
*
* #author Stephen Colebourne
*/
public class Examples {
public static void main(String[] args) throws Exception {
try {
new Examples().run();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
And all the classes for compiling this Example.java is in a joda-time-2.3.jar.
I can successfully compile this program by using
javac -cp somewhere/joda-time-2.3.jar Example.java
And it generate an Example.class, but I jut cannot execute that.
So far I have tried:
java Examples
java -cp somewhere/joda-time-2.3.jar Examples
java -cp somewhere/joda-time-2.3.jar org.joda.example.time.Examples
But they all generate this kind of errors:
Error: Could not find or load main class org.joda.example.time.Example
Error: Could not find or load main class Examples
And I've tried both in the org/joda/example/time folder and the parent folder of org
Anyone can give an instruction on how to execute that? Really appreciate it!
Error: Could not find or load main class org.joda.example.time.Example
public class Examples {
Name of your class is Examples not Example
EDIT
Sorry for late reply...
To execute specific Java program you need to bring control to root directory so if your class is in abc/newdir/Examples.java you need to use cd command (in windows) to lead control to root directory and than compile or you can defeneitly go for the suggestion of kogut.
C:/abc/newdir>java -cp somewhere/joda-time-2.3.jar Examples
Modify your classpath parameter, so it should include directory where Example.class was generated.
In case of out/org/joda/example/time/Example.class you need to use
java -cp somewhere/jodata-time-2.3.jar:out org.joda.example.time.Example
I am trying to execute a .groovy file in Java however being new to both Java and Groovy I am having some problems. I'm doing this to learn more and would appreciate if someone could tell me what i am doing wrong.
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.GroovyShell;
import javax.naming.Binding;
import java.io.File;
public class testClass extends GroovyShell{
public static void main(String[] args){
try{
ClassLoader parent = testClass.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new File("src/testg.groovy"));
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
Object[] args1 = {};
groovyObject.invokeMethod("run",args1);
}catch(Exception e) {
System.out.println("error loading file");
}
}
}
I am getting the following errors :
Groovyc: Cannot compile Groovy files: no Groovy library is defined for module 'Prep'
Using javac 1.7.0_09 to compile java sources
Compilation completed with 1 error and 0 warnings in 9 sec
1 error
0 warnings
Groovyc: Internal groovyc error: code 1
Or perhaps someone could give me an example of how to execute e.g. hello world script written in groovy, in java.
That's not an error thrown by your code. It's an error thrown by IntelliJ, which tries to compile your .groovy file to a .class file.
Since what you want is to parse and run this groovy file at runtime, you shouldn't care about this error. Or rather, to avoid it, you should not put the .groovy file in a directory marked as a source directory in the IntelliJ project, so that IntelliJ doesn't try to compile it.
Sorry for this noobie question, I'm new to Java, and instead of using IDE, i want to using command line to learn what's running under the hood
I'm following the Getting Started guild on MigLayout
#MigWindow.java
public class MigWindow {
public static void main(){
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());// a simple line to make sure the library jar import correctly
}
}
and compile with these command:
javac -cp ./MigLayout.jar MigWindow.java
and I got a error:
MigWindow.java:3: cannot find symbol
symbol : class MigLayout
location: class MigWindow
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());
^
1 error
It seems the jar library doesn't import correctly, any idea?
~
Make sure you add the import for MigLayout
import net.miginfocom.swing.MigLayout;
It may sound obvious, but make sure MigLayout.jar the current directory when calling javac here and that your JAR file has not been corrupted.
Update:
To check that your JAR file does contain the class you can do:
jar tvf MigLayout.jar
and check for the MigLayout class. Failing to find the class you can download the correct one from here.
You are missing an import statement in your Source File. The compiler does not know where 'MigLayout' is coming from.
Add at the top of your file, but below of your package statement (if any) an import, e.g.
import package.MigLayout;
This tells the compiler what to import from the given class path. You will need to replace package with the correct package.
I have 2 .proto files :
First file:
package com.test.model;
message ProtoModel {
required CustomObj custom=1;
}
Second file:
package com.test.model;
message CustomObj {
required string smth=1;
}
The issue here is that "CustomObj" is said to be "unresolved reference" .
Thus, I've tried to import the second file into first file:
import "com/test/model/firstFile.proto"
package com.test.model;
message ProtoModel {
required CustomObj custom=1;
}
I still get the same issue !!
The import statement is the folder relative to the place where you invoke protoc.
It looks like you have treated it as relative to the package instead.
e.g. if (like me) you store both files in src/main/resources, you'd invoke protoc as follows:
protoc src/main/resources/firstFile.proto src/main/resources/secondFile.proto --java_out=src/generated/java
and your import statement would be import "src/main/resources/firstFile.proto"
If you want to store the files in subfolders according to package name, then you just add this accordingly, after the top-level foldername.
HTH